Advertisement
icarussiano

day 6 python

Dec 6th, 2024
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. grid=[list(x) for x in open('input').readlines()]
  2. hash = {(i, j) for i in range(len(grid)) for j  in range(len(grid[i])) if grid[i][j] == '#'}
  3. dirs = { (0, 1): (1, 0),  (1, 0): (0, -1), (0, -1): (-1, 0), (-1, 0): (0, 1) }
  4. start = next((i, j) for i in range(len(grid)) for j in range(len(grid[i])) if grid[i][j] == '^')
  5.  
  6. pos, dir, seen = start, (-1,0), set()
  7. while True:
  8.     seen.add(pos)
  9.     np = (pos[0] + dir[0], pos[1] + dir[1])
  10.     if not (0 <= np[0] < len(grid) and 0 <= np[1] < len(grid[0])):
  11.         break
  12.     if np in hash:
  13.         dir = dirs[dir]
  14.     else:
  15.         pos = np
  16.  
  17. loop=0
  18. for s in seen:
  19.     hashnew=hash | {s}
  20.     pos, dir, old = start, (-1,0), set()
  21.     while True:
  22.         old.add((pos,dir))
  23.         np = (pos[0] + dir[0], pos[1] + dir[1])
  24.         if not (0 <= np[0] < len(grid) and 0 <= np[1] < len(grid[0])):
  25.             break
  26.         if np in hashnew:
  27.             dir = dirs[dir]
  28.         else:
  29.             pos = np
  30.         if (np, dir) in old:
  31.             loop+=1
  32.             break
  33.  
  34.  
  35. print(len(seen))
  36. print(loop)
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement