""" Simulate octopus flashing """ def increment(octopodes, position): for x in range(position[0] - 1, position[0] + 2): if x < 0 or x > 9: continue for y in range(position[1] - 1, position[1] + 2): if y < 0 or y > 9: continue octopodes[y][x] += 1 def add_one(octopodes): for y in range(10): for x in range(10): octopodes[y][x] += 1 def flash(octopodes): flashed = [] flashed_this_iter = True while flashed_this_iter: flashed_this_iter = False for y in range(10): for x in range(10): if octopodes[y][x] > 9 and (x, y) not in flashed: flashed_this_iter = True flashed.append((x, y)) increment(octopodes, (x, y)) for x, y in flashed: octopodes[y][x] = 0 return len(flashed) octopodes = [] with open("11_input.txt", "r") as f: for line in f: octopodes_in_line = [] for char in line.strip(): octopodes_in_line.append(int(char)) octopodes.append(octopodes_in_line) total_flashes = 0 for gen in range(100): add_one(octopodes) total_flashes += flash(octopodes) print(total_flashes)