From 21f671daed35eb7edfb611f02e4e05edce8e5875 Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 6 Dec 2022 19:34:50 +0100 Subject: [PATCH] [py] Port 2017_01 --- py/2017/01/solve.py | 37 ------------------------------------- py/aoc/__init__.py | 2 ++ py/aoc/y2017/d01.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 37 deletions(-) delete mode 100644 py/2017/01/solve.py create mode 100644 py/aoc/y2017/d01.py diff --git a/py/2017/01/solve.py b/py/2017/01/solve.py deleted file mode 100644 index 2b82214..0000000 --- a/py/2017/01/solve.py +++ /dev/null @@ -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) diff --git a/py/aoc/__init__.py b/py/aoc/__init__.py index 62fe93e..6cf769e 100644 --- a/py/aoc/__init__.py +++ b/py/aoc/__init__.py @@ -2,12 +2,14 @@ import sys import argparse from pathlib import Path +from .y2017 import d01 from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11 from .y2020 import d10 from .y2021 import d14 from .y2022 import d01, d02, d03, d04, d05, d06 DAYS = { + "2017_01": y2017.d01.solve, "2018_01": y2018.d01.solve, "2018_02": y2018.d02.solve, "2018_03": y2018.d03.solve, diff --git a/py/aoc/y2017/d01.py b/py/aoc/y2017/d01.py new file mode 100644 index 0000000..eb8589e --- /dev/null +++ b/py/aoc/y2017/d01.py @@ -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}")