Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from adventlib import *
- from collections import defaultdict, deque
- import copy
- import re
- def step(g, xr, yr):
- ng = {}
- moved = False
- for pos, v in g.items():
- # only consider east facing
- if v != '>':
- continue
- # move this guy and do bounds check
- l = pos[0] + 1, pos[1]
- if l[0] >= xr:
- l = 0, l[1]
- # is someone in this slot?
- if g.get(l, '.') in '>v':
- ng[pos] = '>'
- else:
- moved = True
- ng[l] = '>'
- for pos, v in g.items():
- # only consider south facing
- if v != 'v':
- continue
- # move, bounds check in y
- l = pos[0], pos[1] + 1
- if l[1] >= yr:
- l = l[0], 0
- # we need to see if any unmoved vs are below us (since they will move)
- # or see if an east one moved into our spot in the east pass (check ng instead of grid)
- if g.get(l, '.') == 'v' or ng.get(l, '.') == '>':
- ng[pos] = 'v'
- else:
- moved = True
- ng[l] = 'v'
- return moved, ng
- def main(f):
- data = lines(f)
- xr = len(data[0])
- yr = len(data)
- grid = gridify(f)
- i = 0
- for _ in range(1000):
- moved, grid = step(grid, xr, yr)
- i += 1
- if not moved:
- print(i)
- break
- run_main(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement