aoc

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

b.py (1592B) [raw]


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