Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Wind:
- def __init__(self,x,y,dx,dy):
- self.x = x
- self.y = y
- self.dx = dx
- self.dy = dy
- def move(self):
- self.x=(self.x+self.dx)%asizex
- self.y=(self.y+self.dy)%asizey
- def __repr__(self):
- return f"{self.x},{self.y} {self.dx},{self.dy}"
- decode={'v': (0,1), '^': (0,-1), '>': (1,0), '<': (-1,0)}
- deltas=[0,-1,0,1,0]
- infile=open("wind.txt")
- lines=infile.read().split("\n")
- asizex=len(lines[0])-2
- asizey=len(lines)-2
- start=(lines[0].find('.')-1,-1)
- end=(lines[-1].find('.')-1, asizey)
- winds=[Wind(x,y, *decode[ch])
- for y, line in enumerate(lines[1:-1]) for x,ch in enumerate(line[1:-1]) if ch != '.']
- def nav(s,e):
- tokens={s}
- steps=0
- while e not in tokens:
- steps+=1
- nt=set()
- for token in tokens:
- nt.add(token)
- for i in range(4):
- nx=token[0]+deltas[i]
- ny=token[1]+deltas[i+1]
- if (0<=ny<asizey and 0<=nx<asizex) or (nx,ny)==e:
- nt.add((nx,ny))
- for wi, wind in enumerate(winds):
- wind.move()
- nt.discard((wind.x,wind.y))
- tokens=nt
- return steps
- print(nav(start,end)+nav(end,start)+nav(start,end))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement