11.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from util import get_input
  2. input = get_input("11.input")
  3. grid = [[int(a) for a in line] for line in input]
  4. def run_steps(grid, n=100):
  5. tot_flash = 0
  6. for i in range(n):
  7. grid = [[a + 1 for a in row] for row in grid]
  8. flashed = set()
  9. while True:
  10. did_flash = False
  11. for x, row in enumerate(grid):
  12. for y, a in enumerate(row):
  13. if a > 9 and (x, y) not in flashed:
  14. did_flash = True
  15. flashed = flashed.union(set([(x, y)]))
  16. for dx, dy in [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]:
  17. if x + dx < 0 or y + dy < 0:
  18. continue
  19. try:
  20. grid [x + dx] [y + dy] += 1
  21. except IndexError:
  22. continue
  23. if not did_flash:
  24. break
  25. for (x, y) in list(flashed):
  26. grid[x][y] = 0
  27. tot_flash += len(flashed)
  28. if len(flashed) == len(grid) * len(grid[0]):
  29. print("Part 2:", i + 1)
  30. return 0
  31. return tot_flash
  32. print("Part 1:", run_steps(grid, 100))
  33. run_steps(grid, 1000)