-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart2.py
More file actions
38 lines (26 loc) · 759 Bytes
/
part2.py
File metadata and controls
38 lines (26 loc) · 759 Bytes
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
with open("input.txt") as file:
octopuses = [list(map(int, line.strip())) for line in file]
n = len(octopuses)
def ignite(i, j):
octopuses[i][j] += 1
if octopuses[i][j] <= 9:
return 0
octopuses[i][j] = float("-inf")
return 1 + sum(
ignite(new_i, new_j)
for new_i in range(max(0, i - 1), min(n, i + 2))
for new_j in range(max(0, j - 1), min(n, j + 2))
)
step = flashes = 0
target_flashes = n * n
while flashes != target_flashes:
flashes = 0
step += 1
for i in range(n):
for j in range(n):
flashes += ignite(i, j)
for i in range(n):
for j in range(n):
if octopuses[i][j] == float("-inf"):
octopuses[i][j] = 0
print(step)