Advertisement
Guest User

Untitled

a guest
Dec 24th, 2022
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | Source Code | 0 0
  1. class Wind:
  2.     def __init__(self,x,y,dx,dy):
  3.         self.x = x
  4.         self.y = y
  5.         self.dx = dx
  6.         self.dy = dy
  7.     def move(self):
  8.         self.x=(self.x+self.dx)%asizex
  9.         self.y=(self.y+self.dy)%asizey
  10.     def __repr__(self):
  11.         return f"{self.x},{self.y} {self.dx},{self.dy}"
  12.        
  13. decode={'v': (0,1), '^': (0,-1), '>': (1,0), '<': (-1,0)}
  14. deltas=[0,-1,0,1,0]
  15.  
  16. infile=open("wind.txt")
  17. lines=infile.read().split("\n")
  18. asizex=len(lines[0])-2
  19. asizey=len(lines)-2
  20. start=(lines[0].find('.')-1,-1)
  21. end=(lines[-1].find('.')-1, asizey)
  22. winds=[Wind(x,y, *decode[ch])
  23.        for y, line in enumerate(lines[1:-1]) for x,ch in enumerate(line[1:-1]) if ch != '.']
  24. def nav(s,e):
  25.     tokens={s}
  26.     steps=0
  27.     while e not in tokens:
  28.         steps+=1
  29.         nt=set()
  30.         for token in tokens:
  31.             nt.add(token)
  32.             for i in range(4):
  33.                 nx=token[0]+deltas[i]
  34.                 ny=token[1]+deltas[i+1]
  35.                 if (0<=ny<asizey and 0<=nx<asizex) or (nx,ny)==e:
  36.                     nt.add((nx,ny))
  37.         for wi, wind in enumerate(winds):
  38.             wind.move()
  39.             nt.discard((wind.x,wind.y))
  40.         tokens=nt
  41.     return steps
  42. print(nav(start,end)+nav(end,start)+nav(start,end))
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement