aoc2020/15.py

36 lines
680 B
Python

ex=[0,3,6]
def iter(start):
spoke=dict()
lastSpokeD=dict()
turn=0
nextSpeak=None
for i in start:
turn+=1
spoke[i]=turn
yield i
nextSpeak=0
while True:
turn+=1
yield nextSpeak
lastSpoke=spoke.get(nextSpeak)
spoke[nextSpeak]=turn
if lastSpoke is None:
nextSpeak=0
else:
nextSpeak=turn-lastSpoke
if turn%3000000==0:
print(f"turn {turn} of 30000000 ({100*turn/30000000.0}%)")
import itertools
print(list(itertools.islice(iter(ex), 10)))
def nth(start, n):
return next(x for i,x in enumerate(start) if i==n-1)
# print(nth(iter([0,3,6]), 30000000))
print(nth(iter([5,1,9,18,13,8,0]), 30000000))