Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- maze_graph=defaultdict(set)
- the_plan=[j.strip() for j in open("23.txt")]
- directions={(0,-1):'^',(0,1):'v',(-1,0):'<',(1,0):'>'}
- start=the_plan[0].find('.')
- end=the_plan[-1].find('.')
- pos=[[(start,0),(0,1),(start,1)]]
- def avail(point,direction):
- direct=directions.copy()
- out=[]
- direct.pop((direction[0]*-1,direction[1]*-1))
- for d in direct:
- out.append([(point[0]+d[0],point[1]+d[1]),d])
- return list(filter(lambda x: the_plan[x[0][1]][x[0][0]]!='#',out))
- def new_path_helper(list_of_pos):
- for i in list_of_pos:
- point,direction=i
- if the_plan[point[1]][point[0]]==directions[direction] or the_plan[point[1]][point[0]]=='.':
- yield i
- def bfs():
- queue=[]
- ans=[]
- queue.append((1,maze_graph[(start,0)]))
- while queue:
- length,nodes=queue.pop(0)
- for node in nodes:
- l,point=node
- queue.append((length+l,maze_graph[point]))
- if len(nodes)==0:
- ans.append(length)
- return(ans)
- for i in pos:
- origin,direction,point=i
- length=0
- while True:
- if point==(end,len(the_plan)-1):
- maze_graph[origin].add((length,point))
- break
- new=avail(point,direction)
- length+=1
- if len(new)==1:
- point,direction=new[0]
- else:
- maze_graph[origin].add((length,point))
- new=new_path_helper(new)
- for j in new:
- if [point,j[1],j[0]] not in pos:
- pos.append([point,j[1],j[0]])
- else:
- break
- break
- print(max(bfs()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement