Advertisement
Guest User

Untitled

a guest
Dec 24th, 2021
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. from adventlib import *
  2. from collections import defaultdict, deque
  3. import copy
  4. import re
  5.  
  6. def step(g, xr, yr):
  7. ng = {}
  8. moved = False
  9. for pos, v in g.items():
  10. # only consider east facing
  11. if v != '>':
  12. continue
  13.  
  14. # move this guy and do bounds check
  15. l = pos[0] + 1, pos[1]
  16. if l[0] >= xr:
  17. l = 0, l[1]
  18.  
  19. # is someone in this slot?
  20. if g.get(l, '.') in '>v':
  21. ng[pos] = '>'
  22. else:
  23. moved = True
  24. ng[l] = '>'
  25.  
  26. for pos, v in g.items():
  27. # only consider south facing
  28. if v != 'v':
  29. continue
  30.  
  31. # move, bounds check in y
  32. l = pos[0], pos[1] + 1
  33. if l[1] >= yr:
  34. l = l[0], 0
  35.  
  36. # we need to see if any unmoved vs are below us (since they will move)
  37. # or see if an east one moved into our spot in the east pass (check ng instead of grid)
  38. if g.get(l, '.') == 'v' or ng.get(l, '.') == '>':
  39. ng[pos] = 'v'
  40. else:
  41. moved = True
  42. ng[l] = 'v'
  43.  
  44. return moved, ng
  45.  
  46. def main(f):
  47. data = lines(f)
  48. xr = len(data[0])
  49. yr = len(data)
  50.  
  51. grid = gridify(f)
  52. i = 0
  53. for _ in range(1000):
  54. moved, grid = step(grid, xr, yr)
  55. i += 1
  56. if not moved:
  57. print(i)
  58. break
  59.  
  60. run_main(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement