]> git.friedersdorff.com Git - max/advent_of_code_2021.git/blob - 11_2.py
Try day 14
[max/advent_of_code_2021.git] / 11_2.py
1 """ Simulate octopus flashing """
2 import time
3 import random
4
5 def increment(octopodes, position):
6     for x in range(position[0] - 1, position[0] + 2):
7         if x < 0 or x > 39:
8             continue
9
10         for y in range(position[1] - 1, position[1] + 2):
11             if y < 0 or y > 19:
12                 continue
13
14             octopodes[y][x] += 1
15
16
17 def add_one(octopodes):
18     for y in range(20):
19         for x in range(40):
20             octopodes[y][x] += 1
21
22
23 def flash(octopodes):
24     flashed = []
25
26     flashed_this_iter = True
27     while flashed_this_iter:
28         flashed_this_iter = False
29         for y in range(20):
30             for x in range(40):
31                 if octopodes[y][x] > 9 and (x, y) not in flashed:
32                     flashed_this_iter = True
33                     flashed.append((x, y))
34                     increment(octopodes, (x, y))
35
36     for x, y in flashed:
37         octopodes[y][x] = 0
38
39     for y in range(20):
40         display_row = []
41         for x in range(40):
42             if (x, y) in flashed:
43                 display_row.append("X")
44             else:
45                 display_row.append(".")
46         print("".join(display_row))
47
48
49     return len(flashed)
50
51
52 octopodes = []
53 with open("11_input.txt", "r") as f:
54     for line in f:
55         octopodes_in_line = []
56         for char in line.strip():
57             octopodes_in_line.append(int(char))
58
59         octopodes.append(octopodes_in_line)
60
61
62 for gen in range(1, 100000000):
63     add_one(octopodes)
64     print("###", gen, "###")
65     if flash(octopodes) == 100:
66         break
67
68     print()
69     print()
70     time.sleep(1/12)
71
72 print(gen)