aoc2021/d11/d11_1.py

30 lines
758 B
Python
Raw Normal View History

2021-12-20 21:41:29 +00:00
import sys
L = list(map(lambda s: [-999]+list(map(int, s))+[-999] ,sys.stdin.read().splitlines()))
L = [[-999]*12] + L + [[-999]*12]
def printgrid():
for row in L[1:-1]:
print("".join(map(str,row[1:-1])))
printgrid()
N = 0
for s in range(100):
D = set()
print("step",s+1)
for x in range(1,11):
for y in range(1,11):
L[x][y] += 1
flash = True
while flash:
flash = False
for x in range(1,11):
for y in range(1,11):
curr = L[x][y]
if curr > 9:
flash = True
for (i,j) in [(0,1),(1,0),(-1,0),(0,-1),(1,1),(1,-1),(-1,1),(-1,-1)]:
L[x+i][y+j] += 1 if (x+i,y+j) not in D else 0
N+=1
L[x][y] = 0
D.add((x,y)) # can't increase its energy for the rest of the turn
printgrid()
print(N)