summaryrefslogtreecommitdiffstats
path: root/20/py/d15.py
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2020-12-15 07:18:57 +0100
committerGustav Sörnäs <gustav@sornas.net>2020-12-15 07:21:25 +0100
commita02bf755c8ddb81f69afe2514ae6b700ea00f08f (patch)
tree4a02512b61b98204def8f4b52ee613c4b3159f31 /20/py/d15.py
parent6226afb16d4d87226ae73babfce58864b4e3e308 (diff)
downloadaoc-a02bf755c8ddb81f69afe2514ae6b700ea00f08f.tar.gz
d15p2, refactor out
Diffstat (limited to '20/py/d15.py')
-rw-r--r--20/py/d15.py35
1 files changed, 13 insertions, 22 deletions
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))