-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathday15.py
38 lines (29 loc) · 881 Bytes
/
day15.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def play(starting_nums: list[int], stop_at: int) -> int:
mem = {}
for ix, num in enumerate(starting_nums):
mem[num] = (-1, ix+1) # (previous, latest)
ix = len(starting_nums)
latest = starting_nums[-1]
while ix < stop_at:
ix += 1
if (latest not in mem) or (mem[latest][0] == -1):
latest = 0
mem[latest] = (mem[latest][1], ix)
else:
latest = mem[latest][1] - mem[latest][0]
if latest not in mem:
mem[latest] = (-1, ix)
else:
mem[latest] = (mem[latest][1], ix)
return latest
def part1(data):
return play(data, 2020)
def part2(data):
return play(data, 30000000)
with open('day15-data.txt') as f:
inputs = [
int(i)
for i in f.read().split(',')
]
print(part1(inputs))
print(part2(inputs))