From a02bf755c8ddb81f69afe2514ae6b700ea00f08f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 15 Dec 2020 07:18:57 +0100 Subject: d15p2, refactor out --- 20/py/d15.py | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) (limited to '20/py') diff --git a/20/py/d15.py b/20/py/d15.py index 8723a28..b8e41fb 100644 --- a/20/py/d15.py +++ b/20/py/d15.py @@ -3,28 +3,11 @@ import aoc20 import sys -def pt1(_in): - nums = [int(n) for n in _in[0].split(",")] - prev = {n: i for i, n in enumerate(nums)} - while len(nums) < 2020: - cur = nums[-1] - if cur not in prev: - nums.append(0) - prev[cur] = len(nums) - 2 - else: - nums.append(len(nums) - 1 - prev[cur]) - prev[cur] = len(nums) - 2 - return nums[2019] - - -def pt2(_in): - nums = [int(n) for n in _in[0].split(",")] - amount = 3 - cur = nums[-1] - prev = {n: i for i, n in enumerate(nums)} - while amount < 30000000: - if amount % 300000 == 0: - print(amount // 300000) +def say_nth(start, nth): + amount = len(start) + cur = start[-1] + prev = {n: i for i, n in enumerate(start)} + while amount < nth: if cur not in prev: new = 0 prev[cur] = amount - 1 @@ -36,6 +19,14 @@ def pt2(_in): return cur +def pt1(_in): + return say_nth([int(n) for n in _in[0].split(",")], 2020) + + +def pt2(_in): + return say_nth([int(n) for n in _in[0].split(",")], 30000000) + + if __name__ == "__main__": input = aoc20.read_input(sys.argv[1:], 15) print(pt1(input)) -- cgit v1.2.1