From 5bc5c4042ee6915d6b762b72c5dc212e837fee37 Mon Sep 17 00:00:00 2001 From: dece Date: Wed, 9 Dec 2020 13:25:32 +0100 Subject: [PATCH] Day 9 --- 2020/day9.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2020/day9.py diff --git a/2020/day9.py b/2020/day9.py new file mode 100644 index 0000000..d733bf2 --- /dev/null +++ b/2020/day9.py @@ -0,0 +1,32 @@ +import itertools + + +def main(): + with open("day9.txt", "rt") as f: + series = [int(line.rstrip()) for line in f.readlines()] + + # Part 1 + frame_len = 25 + for i in range(len(series) - frame_len): + frame = series[i : i + frame_len] + n = series[i + frame_len] + for a, b in itertools.combinations(frame, 2): + if a + b == n: + break + else: + print("Invalid N:", n) + break + + # Part 2 + for i in range(len(series)): # what is indexerror when you know it breaks + for j in range(1, len(series)): + frame = series[i : i + j + 1] + if (s := sum(frame)) == n: + print("Answer:", min(frame) + max(frame)) + return + elif s > n: + break + + +if __name__ == "__main__": + main()