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/README | 2 ++ 20/py/d15.py | 35 +++++++++++++---------------------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/20/README b/20/README index 4de8366..9260dce 100644 --- a/20/README +++ b/20/README @@ -16,6 +16,7 @@ Day Time Ans Time Ans 12 0.257 1645 0.279 35292 13 0.006 2095 0.018 598411311431841 14 4.357 15018100062885 55.561 5724245857696 + 15 0.363 232 10516.154 18929178 ------- ------- tot TBD ms TBD ms @@ -24,6 +25,7 @@ Stats: -------Part 1-------- -------Part 2-------- Day Time Rank Score Time Rank Score + 15 00:59:43 5655 0 01:17:06 4904 0 14 00:43:31 4037 0 01:00:44 2402 0 13 01:16:45 7549 0 02:27:40 3806 0 12 00:09:08 645 0 00:16:03 373 0 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