aocpy/2020/d15.py
2024-09-15 12:26:11 -04:00

23 lines
658 B
Python

from lib import get_data, str_to_ints
from collections import defaultdict
data = get_data(__file__)
for limit in [2021, 30000001]:
xs = list(reversed(str_to_ints(data)))
spoken = defaultdict(list)
recent = None
for turn in range(1, limit):
if len(xs) > 0:
recent = xs.pop()
spoken[recent].append(turn)
else:
if recent in spoken and len(spoken[recent]) == 1:
recent = 0
elif recent in spoken:
recent = spoken[recent][-1] - spoken[recent][-2]
else:
recent = 0
spoken[recent].append(turn)
print(recent)