#!/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()]) flashes = 0 for i in range(100): flashes += energy_simulate(cavern) print(flashes)