Solve 2020 day 15

This commit is contained in:
2024-09-15 12:26:11 -04:00
parent 7986ecfc21
commit db383c7cfa
2 changed files with 24 additions and 1 deletions

22
2020/d15.py Normal file
View File

@@ -0,0 +1,22 @@
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)