You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

p25.py 642B

3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324
  1. text = open(0).read()
  2. state = {}
  3. for y, row in enumerate(text.splitlines()):
  4. for x, ch in enumerate(row):
  5. state[x, y] = ch
  6. X, Y = x + 1, y + 1
  7. seen = set()
  8. for turn in range(1000):
  9. for ch, dx, dy in [('>', 1, 0), ('v', 0, 1)]:
  10. copy = state.copy()
  11. for old in copy:
  12. if copy[old] == ch:
  13. x, y = old
  14. new = (x + dx) % X, (y + dy) % Y
  15. if copy[new] == '.':
  16. state[old] = '.'
  17. state[new] = ch
  18. hashable = frozenset(state.items())
  19. if hashable in seen:
  20. print(turn + 1)
  21. break
  22. seen.add(hashable)