]> git.friedersdorff.com Git - max/advent_of_code_2021.git/blob - 11_1.py
Try day 14
[max/advent_of_code_2021.git] / 11_1.py
1 """ Simulate octopus flashing """
2
3 def increment(octopodes, position):
4     for x in range(position[0] - 1, position[0] + 2):
5         if x < 0 or x > 9:
6             continue
7
8         for y in range(position[1] - 1, position[1] + 2):
9             if y < 0 or y > 9:
10                 continue
11
12             octopodes[y][x] += 1
13
14
15 def add_one(octopodes):
16     for y in range(10):
17         for x in range(10):
18             octopodes[y][x] += 1
19
20
21 def flash(octopodes):
22     flashed = []
23
24     flashed_this_iter = True
25     while flashed_this_iter:
26         flashed_this_iter = False
27         for y in range(10):
28             for x in range(10):
29                 if octopodes[y][x] > 9 and (x, y) not in flashed:
30                     flashed_this_iter = True
31                     flashed.append((x, y))
32                     increment(octopodes, (x, y))
33
34     for x, y in flashed:
35         octopodes[y][x] = 0
36
37     return len(flashed)
38
39
40 octopodes = []
41 with open("11_input.txt", "r") as f:
42     for line in f:
43         octopodes_in_line = []
44         for char in line.strip():
45             octopodes_in_line.append(int(char))
46
47         octopodes.append(octopodes_in_line)
48
49 total_flashes = 0
50 for gen in range(100):
51     add_one(octopodes)
52     total_flashes += flash(octopodes)
53
54 print(total_flashes)