adventofcode-2021/11/solve02.py

55 lines
1.0 KiB
Python
Raw Normal View History

2021-12-11 09:08:18 +01:00
#!/usr/bin/env python
def print_cavern(cavern):
for l in cavern:
print("".join([str(i) for i in l]))
def energy_simulate(cavern):
# increase energy level
for x in range(10):
for y in range(10):
cavern[y][x] += 1
# check flashes
flashed = []
while True:
flashed_round = 0
for x in range(10):
for y in range(10):
if cavern[y][x] < 10:
continue
if (x,y) in flashed:
continue
flashed_round += 1
flashed.append((x,y))
for d in ((0,1), (0,-1),(1,0),(-1,0), (1,1),(1,-1),(-1,1),(-1,-1)):
dx = x + d[0]
dy = y + d[1]
if (dx < 0) or (dy < 0):
continue
if (dx > 9) or (dy > 9):
continue
cavern[dy][dx] += 1
if flashed_round == 0:
break
# flashed set to 0
for d in flashed:
cavern[d[1]][d[0]] = 0
return len(flashed)
cavern = []
with open("input01.txt","r") as f:
for line in f:
cavern.append([int(x) for x in line.strip()])
round = 0
while True:
round += 1
flashes = energy_simulate(cavern)
if flashes == 100:
break
print(round)