blob: a5660a4eda523a48ab6fa8021be5084e0055ed50 (
plain)
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
|
"""Day 15."""
# reading off numbers
# starting numbers in input
# consider the most recently spoken number
# new unique numbers mean the current says zero
# how far aprat the previous spoken one was
with open("input") as f:
memory = list(map(int, f.read().rstrip().split(",")))
last_pos = {val: pos + 1 for pos, val in enumerate(memory)}
pos = len(memory) + 1
for x in range(2020 - pos + 1):
last = memory[-1]
if memory.count(last) == 1:
memory.append(0)
else:
x = pos - 1
y = last_pos[last]
val = x - y
memory.append(val)
last_pos[last] = x
if val not in last_pos:
last_pos[val] = pos
pos += 1
print(memory[-1])
|