From 521cef0e58239c51141f3dfafb5672cd6bd00f84 Mon Sep 17 00:00:00 2001 From: Dece Date: Sun, 22 Dec 2019 01:41:19 +0100 Subject: [PATCH] 2018 day 1 part 2 --- 2018/day1.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/2018/day1.py b/2018/day1.py index e379f6c..af07a4b 100644 --- a/2018/day1.py +++ b/2018/day1.py @@ -1,3 +1,19 @@ import sys -print(sum(int(line.rstrip()) for line in sys.stdin.readlines())) +changes = [int(line.rstrip()) for line in sys.stdin.readlines()] + +# Part 1 +print("Simple sum:", sum(changes)) + +# Part 2 +freq = 0 +history = {0: True} +found = False +while not found: + for c in changes: + freq += c + if freq in history: + found = True + break + history[freq] = True +print("First repeated:", freq)