[py] Port 2017_01

This commit is contained in:
Joscha 2022-12-06 19:34:50 +01:00
parent beee256111
commit 21f671daed
3 changed files with 36 additions and 37 deletions

View file

@ -1,37 +0,0 @@
import sys
def load_line(filename):
with open(filename, "r") as f:
return list(map(int, f.read()[:-1]))
# PART 1
def sum_matching(digits):
offset = digits[1:] + digits
total = 0
for x, y in zip(digits, offset):
if x == y:
total += x
return total
# PART 2
def sum_matching_2(digits):
offset = digits[len(digits)//2:] + digits
total = 0
for x, y in zip(digits, offset):
if x == y:
total += x
return total
def main(filename):
digits = load_line(filename)
print(f"Solutions for {filename}")
total = sum_matching(digits)
print(f"Part 1: {total}")
total_2 = sum_matching_2(digits)
print(f"Part 2: {total_2}")
if __name__ == "__main__":
for filename in sys.argv[1:]:
main(filename)

View file

@ -2,12 +2,14 @@ import sys
import argparse import argparse
from pathlib import Path from pathlib import Path
from .y2017 import d01
from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11 from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11
from .y2020 import d10 from .y2020 import d10
from .y2021 import d14 from .y2021 import d14
from .y2022 import d01, d02, d03, d04, d05, d06 from .y2022 import d01, d02, d03, d04, d05, d06
DAYS = { DAYS = {
"2017_01": y2017.d01.solve,
"2018_01": y2018.d01.solve, "2018_01": y2018.d01.solve,
"2018_02": y2018.d02.solve, "2018_02": y2018.d02.solve,
"2018_03": y2018.d03.solve, "2018_03": y2018.d03.solve,

34
py/aoc/y2017/d01.py Normal file
View file

@ -0,0 +1,34 @@
def load_line(inputstr):
return list(map(int, inputstr[:-1]))
# PART 1
def sum_matching(digits):
offset = digits[1:] + digits
total = 0
for x, y in zip(digits, offset):
if x == y:
total += x
return total
# PART 2
def sum_matching_2(digits):
offset = digits[len(digits) // 2 :] + digits
total = 0
for x, y in zip(digits, offset):
if x == y:
total += x
return total
def solve(inputstr):
digits = load_line(inputstr)
total = sum_matching(digits)
print(f"Part 1: {total}")
total_2 = sum_matching_2(digits)
print(f"Part 2: {total_2}")