From 31d6098c580c712927ecadc04cbc3d0463b712e2 Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 6 Dec 2022 15:16:01 +0100 Subject: [PATCH] [py] Port 2018_01 --- py/2018/01/solve.py | 33 --------------------------------- py/2018/01/test_input.txt | 5 ----- py/aoc/__init__.py | 2 ++ py/aoc/y2018/d01.py | 17 +++++++++++++++++ 4 files changed, 19 insertions(+), 38 deletions(-) delete mode 100644 py/2018/01/solve.py delete mode 100644 py/2018/01/test_input.txt create mode 100644 py/aoc/y2018/d01.py diff --git a/py/2018/01/solve.py b/py/2018/01/solve.py deleted file mode 100644 index 1bc1946..0000000 --- a/py/2018/01/solve.py +++ /dev/null @@ -1,33 +0,0 @@ -import sys - -def load_freqs(filename): - freqs = [] - with open(filename, "r") as f: - for line in f: - n = int(line[:-1]) - freqs.append(n) - return freqs - -# PART 2 - -def find_repeat(freqs): - total = 0 - found = {total} - - while True: - for n in freqs: - total += n - if total in found: - return total - else: - found.add(total) - -def main(filename): - freqs = load_freqs(filename) - print(f"Solutions for {filename}") - print(f"Part 1: {sum(freqs)}") - print(f"Part 2: {find_repeat(freqs)}") - -if __name__ == "__main__": - for filename in sys.argv[1:]: - main(filename) diff --git a/py/2018/01/test_input.txt b/py/2018/01/test_input.txt deleted file mode 100644 index 021abef..0000000 --- a/py/2018/01/test_input.txt +++ /dev/null @@ -1,5 +0,0 @@ -+1 -+2 --3 --4 -+5 diff --git a/py/aoc/__init__.py b/py/aoc/__init__.py index 477a115..4cbb592 100644 --- a/py/aoc/__init__.py +++ b/py/aoc/__init__.py @@ -2,11 +2,13 @@ import sys import argparse from pathlib import Path +from .y2018 import d01 from .y2020 import d10 from .y2021 import d14 from .y2022 import d01, d02, d03, d04, d05, d06 DAYS = { + "2018_01": y2018.d01.solve, "2020_10": y2020.d10.solve, "2021_14": y2021.d14.solve, "2022_01": y2022.d01.solve, diff --git a/py/aoc/y2018/d01.py b/py/aoc/y2018/d01.py new file mode 100644 index 0000000..9e436f5 --- /dev/null +++ b/py/aoc/y2018/d01.py @@ -0,0 +1,17 @@ +def find_repeat(freqs): + total = 0 + found = {total} + + while True: + for n in freqs: + total += n + if total in found: + return total + else: + found.add(total) + + +def solve(inputstr): + freqs = [int(freq) for freq in inputstr.splitlines()] + print(f"Part 1: {sum(freqs)}") + print(f"Part 2: {find_repeat(freqs)}")