-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday11-dictionary.py
54 lines (37 loc) · 1.37 KB
/
day11-dictionary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# positions are represented as complex numbers
# x, y = x+yj
import numpy as np
octopuses = {x + y * 1j: int(energy)
for x, line in enumerate(open('2021//input//day11.txt'))
for y, energy in enumerate(line.strip())}
def print_board(octopuses):
line = ""
for octopus in octopuses:
if octopus.imag == 0j:
print(line)
line = str(octopuses[octopus]) + " "
else:
line += str(octopuses[octopus]) + " "
print(line)
def step(octopuses):
# increase all the energies
for octopus in octopuses:
octopuses[octopus] += 1
step_flashes = 0
# keep flashing and increasing neighbors until no one is flashing anymore
while (flashes := sum(flash(octopuses, octopus) for octopus, energy in octopuses.items() if energy > 9)):
step_flashes += flashes
return step_flashes
def flash(octopuses, octopus):
# reset the current octopus
octopuses[octopus] = 0
# increase the energy of all the neighbors
for neighbor in np.array([-1, 1, -1j, 1j, -1 - 1j, -1 + 1j, 1 + 1j, 1 - 1j]) + octopus:
if neighbor in octopuses and octopuses[neighbor] > 0:
octopuses[neighbor] += 1
return 1
print("Part 1:", sum(step(octopuses) for _ in range(100)))
current_step = 100
while step(octopuses) != 100:
current_step += 1
print("Part 2:", current_step + 1)