""" Simulate octopus flashing """ import time import random def increment(octopodes, position): for x in range(position[0] - 1, position[0] + 2): if x < 0 or x > 39: continue for y in range(position[1] - 1, position[1] + 2): if y < 0 or y > 19: continue octopodes[y][x] += 1 def add_one(octopodes): for y in range(20): for x in range(40): octopodes[y][x] += 1 def flash(octopodes): flashed = [] flashed_this_iter = True while flashed_this_iter: flashed_this_iter = False for y in range(20): for x in range(40): 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 for y in range(20): display_row = [] for x in range(40): if (x, y) in flashed: display_row.append("X") else: display_row.append(".") print("".join(display_row)) 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) for gen in range(1, 100000000): add_one(octopodes) print("###", gen, "###") if flash(octopodes) == 100: break print() print() time.sleep(1/12) print(gen)