Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

45 lines
1.1KB

  1. def loop(p, drx):
  2. p += drx
  3. if p not in grid:
  4. p -= drx * (H if drx.imag else W)
  5. return p
  6. def blizzards(n, cache={}, valid={'>': 1, '<': -1, '^': -1j, 'v': 1j}):
  7. if n not in cache:
  8. if n == 0:
  9. cache[n] = [(p, valid[c]) for p, c in grid.items() if c != '.']
  10. else:
  11. cache[n] = [(loop(p, drx), drx) for p, drx in blizzards(n - 1)]
  12. return cache[n]
  13. def bfs(starts, ends, tt=0, dir5=[0, 1, -1j, -1, 1j]):
  14. edge = starts
  15. while not ends & edge:
  16. tt += 1
  17. blizz = {p for p, _ in blizzards(tt)}
  18. edge = {p + dp for p in edge for dp in dir5} & set(grid) - blizz
  19. return tt
  20. def main():
  21. global grid, W, H
  22. rows = open(0).read().splitlines()
  23. grid = {complex(x, y): c for y, r in enumerate(rows)
  24. for x, c in enumerate(r) if c != '#'}
  25. H, W = len(rows) - 2, len(rows[1]) - 2
  26. start, *_, end = sorted(grid, key=lambda p: (p.imag, p.real))
  27. t1 = bfs({start}, {end})
  28. print(t1)
  29. t2 = bfs({end}, {start}, t1)
  30. t3 = bfs({start}, {end}, t2)
  31. print(t3)
  32. main()