25.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from copy import deepcopy
  2. from util import get_input
  3. input = get_input("25.input")
  4. grid = [[c for c in line] for line in input]
  5. grid_size = (len(grid[0]), len(grid))
  6. def print_grid(grid):
  7. for line in grid:
  8. print("".join(line))
  9. print("-" * 20)
  10. def step(grid):
  11. moved = False
  12. new_grid = deepcopy(grid)
  13. for y, line in enumerate(grid):
  14. for x, c in enumerate(line):
  15. if c == '>' and grid[y][(x + 1) % grid_size[0]] == '.':
  16. moved = True
  17. new_grid[y][x] = '.'
  18. new_grid[y][(x + 1) % grid_size[0]] = '>'
  19. new_new_grid = deepcopy(new_grid)
  20. for y, line in enumerate(new_grid):
  21. for x, c in enumerate(line):
  22. if c == 'v' and new_grid[(y + 1) % grid_size[1]][x] == '.':
  23. moved = True
  24. new_new_grid[y][x] = '.'
  25. new_new_grid[(y + 1) % grid_size[1]][x] = 'v'
  26. return (moved, new_new_grid)
  27. print_grid(grid)
  28. i = 0
  29. while True:
  30. i += 1
  31. moved, grid = step(grid)
  32. if not moved:
  33. break
  34. #print_grid(grid)
  35. print(i)
  36. #print_grid(grid)
  37. print("Part 1:", i)