aoc2021/d11/d11_2.py

33 lines
810 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 = []
for s in range(1000):
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
if len(D) == 100:
print(s)
N.append(s)
printgrid()
print(N[0]+1)