aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

a.py (1579B) [raw]


      1 #!/usr/bin/env python3
      2 import sys
      3 
      4 G = [[0] * 10 for i in range(10)]
      5 flashes = 0
      6 
      7 
      8 def debug(i=0):
      9     print(f"--- {i} ---")
     10     for r in G:
     11         print("".join([str(i) for i in r]))
     12 
     13 
     14 def step():
     15     flashed = [[False] * 10 for i in range(10)]
     16 
     17     def flash(r, c):
     18         global flashes
     19 
     20         # At minimum, receive += 1
     21         G[r][c] += 1
     22 
     23         # Now flash neighbors
     24         if G[r][c] > 9 and not flashed[r][c]:
     25             flashed[r][c] = True
     26             flashes += 1
     27             # Neighbors
     28             if r > 0:
     29                 flash(r - 1, c)
     30             if c > 0:
     31                 flash(r, c - 1)
     32             if r < 9:
     33                 flash(r + 1, c)
     34             if c < 9:
     35                 flash(r, c + 1)
     36             # corners
     37             if r > 0 and c > 0:
     38                 flash(r - 1, c - 1)
     39             if r < 9 and c < 9:
     40                 flash(r + 1, c + 1)
     41             if r > 0 and c < 9:
     42                 flash(r - 1, c + 1)
     43             if r < 9 and c > 0:
     44                 flash(r + 1, c - 1)
     45 
     46     # increase everyone first
     47     for r in range(10):
     48         for c in range(10):
     49             G[r][c] += 1
     50 
     51     # now flash it up
     52     for r in range(10):
     53         for c in range(10):
     54             if G[r][c] > 9:
     55                 flash(r, c)
     56 
     57     # now, reset
     58     for r in range(10):
     59         for c in range(10):
     60             if G[r][c] > 9:
     61                 G[r][c] = 0
     62 
     63 
     64 # Read in the grid
     65 r = 0
     66 for l in sys.stdin:
     67     c = 0
     68     for n in l.strip():
     69         G[r][c] = int(n)
     70         c += 1
     71     r += 1
     72 
     73 for i in range(100):
     74     # debug(i)
     75     step()
     76 
     77 print(flashes)