#!/usr/bin/env python3 import sys G = [[0] * 10 for i in range(10)] flashes = 0 def debug(i=0): print(f"--- {i} ---") for r in G: print("".join([str(i) for i in r])) def step(): flashed = [[False] * 10 for i in range(10)] def flash(r, c): global flashes # At minimum, receive += 1 G[r][c] += 1 # Now flash neighbors if G[r][c] > 9 and not flashed[r][c]: flashed[r][c] = True flashes += 1 # Neighbors if r > 0: flash(r - 1, c) if c > 0: flash(r, c - 1) if r < 9: flash(r + 1, c) if c < 9: flash(r, c + 1) # corners if r > 0 and c > 0: flash(r - 1, c - 1) if r < 9 and c < 9: flash(r + 1, c + 1) if r > 0 and c < 9: flash(r - 1, c + 1) if r < 9 and c > 0: flash(r + 1, c - 1) # increase everyone first for r in range(10): for c in range(10): G[r][c] += 1 # now flash it up for r in range(10): for c in range(10): if G[r][c] > 9: flash(r, c) # now, reset for r in range(10): for c in range(10): if G[r][c] > 9: G[r][c] = 0 # Read in the grid r = 0 for l in sys.stdin: c = 0 for n in l.strip(): G[r][c] = int(n) c += 1 r += 1 for i in range(100): # debug(i) step() print(flashes)