# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + jupyter={"outputs_hidden": true}
from IPython.core.debugger import set_trace
import numpy as np
import sys
import collections
import matplotlib
import matplotlib.pyplot as plt

f=open('day20input.txt').read() #not with read because thats probably the whole file

#f='^WSSEESWWWNW(S|NENNEEEENN(ESSSSW(NWSW|SSEN)|WSWWN(E|WWS(E|SS))))$'
#f='^ENNWSWW(NEWS|)SSSEEN(WNSE|)EE(SWEN|)NNN$' 

# + jupyter={"outputs_hidden": true}
len(f)

# + jupyter={"outputs_hidden": true}
nc = (f.count('N'))
ec = (f.count('E'))
sc = (f.count('S'))
wc = (f.count('W'))
print (nc,ec,sc,wc)

# + jupyter={"outputs_hidden": true}
maze = np.full(shape=(2*max(nc,sc),2*max(ec,wc)),fill_value='#')
#maze = np.full(shape=(10,10),fill_value='#')
splits=[]
startpos = (maze.shape[0]//2,maze.shape[1]//2)
#startpos=(5,5)
pos = startpos
print (pos)
maze[pos]='.'
door = {'N':(-1,0),
       'E':(0,1),
       'S':(1,0),
       'W':(0,-1)}
room = {'N':(-2,0),
       'E':(0,2),
       'S':(2,0),
       'W':(0,-2)}
i=1
while(i<len(f)-1):
    if i%100==0: print (i)
    s=f[i]
    #print (s,pos,s,'splits',splits)
    if s == '$': sys.exit(0)
    elif s in door.keys():
        #print ('wind')
        maze[(pos[0]+door[s][0],pos[1]+door[s][1])]='|'
        maze[(pos[0]+room[s][0],pos[1]+room[s][1])]='.'
        
        pos = (pos[0]+room[s][0],pos[1]+room[s][1])
        
    elif s == '|':
        #print ('go back to last split')
        pos = splits[-1]
    elif s == '(':
        #print ('add a split')
        splits.append(pos)
    elif s == ')':
        #print ('close split')
        pos = splits.pop(-1)
    #print (pos,splits)
    i+=1
#'<ENWWW(NEEE|SSE(EE|N))'




# + jupyter={"outputs_hidden": true}
def neighbors(go):
    result =[]
    for d in door.keys():
        #print ((d,go[0]+door[d][0],go[1]+door[d][1]))
        #print (maze[(go[0]+room[d][0],go[1]+room[d][1])])
        if maze[(go[0]+door[d][0],go[1]+door[d][1])]=='|':
            result.append((go[0]+room[d][0],go[1]+room[d][1]))
    #print(go,result)
    return result


distances = np.zeros(shape=(maze.shape))
steps = 1
togo = [startpos]
seen = [startpos]
#while np.count_nonzero(distances)<np.count_nonzero(maze == '.')-1:
while len(togo)>0:
    #print ('togobegin',togo)
    togonext=[]

    for go in togo:#cell to explore

        #print ('currentcell',go)

        neighborlist = neighbors(go)#check neighbors of this cell
        for n in neighborlist: #for all neighbors
            if n not in seen: #if we didnt already reach them
                distances[n]=steps 
                seen.append(go)
                togonext.append(n)
    togo = togonext.copy()
    steps+=1

        
    

# + jupyter={"outputs_hidden": true}
maze

# + jupyter={"outputs_hidden": true}
np.max(distances)

# + jupyter={"outputs_hidden": true}
np.count_nonzero(np.array(distances>=1000))

# + jupyter={"outputs_hidden": true}
maze

StarcoderdataJupyterScriptsDedupFiltered