<jupyter_start><jupyter_text>Day 15 part A<jupyter_code>import numpy as np
inp=[0,5,4,1,10,14,7]
numbers=np.zeros((2020))
numbers[:]=np.nan
countinp=len(inp)
numbers[:countinp]=inp

for idx in range(countinp,len(numbers)):
    # check if nr was used before:
    nr=numbers[idx-1]
    previdxs=np.nonzero(numbers[:idx-1]==nr)[0]
    if len(previdxs)!=0:
        numbers[idx]=idx-1-previdxs[-1] # prev turn - turn before that
    else:
        numbers[idx]=0
    
numbers
numbers[-1]<jupyter_output><empty_output><jupyter_text>Part Bwe change the way data is being stored: now we only record in which turn numbers were called the last time. we don't save the whole sequence of calls any more<jupyter_code>maxround=30000000
lastturn=np.zeros((maxround),dtype=np.int)-1 # position of the last idx with that value. 
                            # lastturn[4]-1= value 4 was never used.
                            # lastturn[4]=2: value 4 was named in turn 2
# initialize
prevval=None
prevturn=None
for idx,val in enumerate(inp): # (all nrs are unique)
    if idx==0: 
        prevval=val
        prevturn=idx
        continue
    # always write one loop later
    lastturn[prevval]=prevturn
    prevval=val
    prevturn=idx
# now the 7 is stored in prevval but not written yet with the value prevturn=6
# play the game
for idx in range(len(inp),maxround):
    # check if the prevval has been used before (important: the turn count has not been updated yet with the last turn)
    if idx%1000000==0:
        print(idx,prevval)
    if lastturn[prevval]==-1: # was not used before
        newval=0 
    else:
        newval=idx-1-lastturn[prevval]
    
    # store the old prevval
    lastturn[prevval]=prevturn
    
    # create the new prevval
    prevval=newval
    prevturn=idx

# the next value that will be written is the solution
prevval
import matplotlib.pyplot as plt
plt.figure(figsize=(10,5))
plt.plot(np.arange(0,3000000,100),30000000-lastturn[0:3000000:100],".",alpha=0.5)
plt.xlabel("value")
plt.ylabel("how long ago that it was last named");
np.nonzero(lastturn==-1)[0]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1087369420702754, 'fraction_numerical': 0.042735042735042736, 'mean_word_length': 3.4829787234042553, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '558081', 'license': 'MIT', 'n_tokens_mistral': 771, 'n_tokens_neox': 711, 'n_words': 207, 'path': 'advent_of_code_15.ipynb', 'repo_name': 'walkernoreen/adventofcode2020', 'size': '2106'}