-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.py
183 lines (153 loc) · 5.38 KB
/
day11.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""Solution for Advent of Code day 11."""
from pathlib import Path
import doctest
import click
def read_energy_grid(filename: Path) -> list[list[int]]:
"""Read energy grid from file
Args:
filename (Path): path to the input file.
Returns:
list[list[int]] Energy field
Examples:
>>> x = [print(line) for line in read_energy_grid(Path("test_data/day_11.data"))]
[5, 4, 8, 3, 1, 4, 3, 2, 2, 3]
[2, 7, 4, 5, 8, 5, 4, 7, 1, 1]
[5, 2, 6, 4, 5, 5, 6, 1, 7, 3]
[6, 1, 4, 1, 3, 3, 6, 1, 4, 6]
[6, 3, 5, 7, 3, 8, 5, 4, 7, 8]
[4, 1, 6, 7, 5, 2, 4, 6, 4, 5]
[2, 1, 7, 6, 8, 4, 1, 7, 2, 1]
[6, 8, 8, 2, 8, 8, 1, 1, 3, 4]
[4, 8, 4, 6, 8, 4, 8, 5, 5, 4]
[5, 2, 8, 3, 7, 5, 1, 5, 2, 6]
"""
grid = []
with filename.open("r") as file:
for line in file:
grid.append([int(x) for x in line.strip()])
return grid
def flash_octopus(
row: int, col: int, grid: list[list[int]]
) -> tuple[list[list[int]], int]:
"""Flashes single occtopus. (recursively)
If the flash of an occtopus causes another occtopus to flash the function
is called recursivly.
The energy level of the flashed occtopus is reset to -1 to prevent multiple
flashing.
Args:
row (int): row of flashing occtopus
col (int): column of flashing occtopus
grid (list[list[int]]): energy grid
Returns:
list[list[int]]: updated energy grid
int: number of flashes
Examples:
>>> flash_octopus(1,1,[[1,1,1],[1,10,1],[1,1,1]])
([[2, 2, 2], [2, -1, 2], [2, 2, 2]], 1)
>>> flash_octopus(1,1,[[9,1,9],[6,10,8],[8,9,7]])
([[-1, 6, -1], [-1, -1, -1], [-1, -1, -1]], 8)
"""
num_row = len(grid)
num_col = len(grid[0])
flashes = 1
grid[row][col] = -1
for delta_row in [-1, 0, 1]:
for delta_col in [-1, 0, 1]:
row_adjacent = row + delta_row
col_adjacent = col + delta_col
if (
0 <= row_adjacent < num_row
and 0 <= col_adjacent < num_col
and grid[row_adjacent][col_adjacent] != -1
):
grid[row_adjacent][col_adjacent] += 1
if grid[row_adjacent][col_adjacent] >= 10:
grid, flashes_child = flash_octopus(
row_adjacent, col_adjacent, grid
)
flashes += flashes_child
return grid, flashes
def simulate_round(grid: list[list[int]]) -> tuple[list[list[int]], int, bool]:
"""Simulate a single round.
* First, the energy level of each octopus increases by 1.
* Then, any octopus with an energy level greater than 9 flashes. This
increases the energy level of all adjacent octopuses by 1, including
octopuses that are diagonally adjacent. If this causes an octopus to
have an energy level greater than 9, it also flashes. This process
continues as long as new octopuses keep having their energy level
increased beyond 9. (An octopus can only flash at most once per step.)
* Finally, any octopus that flashed during this step has its energy level
set to 0, as it used all of its energy to flash.
Args:
grid (list[list[int]]): energy grid
Returns:
list[list[int]]: updated energy grid.
int: number of flashes.
Examples:
>>> simulate_round([[0,0,0],[0,9,0],[0,0,0]])
([[2, 2, 2], [2, 0, 2], [2, 2, 2]], 1)
>>> simulate_round([[8,0,8],[5,9,7],[7,8,6]])
([[0, 6, 0], [0, 0, 0], [0, 0, 0]], 8)
>>> simulate_round([[8,9,8],[8,9,7],[9,8,8]])
([[0, 0, 0], [0, 0, 0], [0, 0, 0]], 9)
"""
sum_flashes = 0
num_row = len(grid)
num_col = len(grid[0])
for row in range(num_row):
for col in range(num_col):
grid[row][col] += 1
for row in range(num_row):
for col in range(num_col):
if grid[row][col] >= 10:
grid, flashes = flash_octopus(row, col, grid)
sum_flashes += flashes
for row in range(num_row):
for col in range(num_row):
if grid[row][col] == -1:
grid[row][col] = 0
return grid, sum_flashes
@click.group()
def main():
"""CLI for the solution of day 11
Advent of code 2021 (https://adventofcode.com/2021/day/11)
"""
@main.command()
@click.argument(
"filename",
required=False,
type=Path,
default=Path("test_data/day_11.data"),
)
def part_1(filename: Path):
"""Part one of day 11. (sum flashes)"""
grid = read_energy_grid(filename)
sum_flashes = 0
for _ in range(100):
grid, flashes = simulate_round(grid)
sum_flashes += flashes
print(f"After 100 rounds there has been {sum_flashes} flashes.")
@main.command()
@click.argument(
"filename",
required=False,
type=Path,
default=Path("test_data/day_11.data"),
)
def part_2(filename: Path):
"""Part two of day ten. (numer of round before sync)"""
grid = read_energy_grid(filename)
octopuses = len(grid) * len(grid[0])
num_rounds = 0
while True:
num_rounds += 1
grid, flashes = simulate_round(grid)
if flashes == octopuses:
break
print(f"Octopuses synced after {num_rounds} rounds.")
@main.command()
def test():
"""run doctest."""
print(doctest.testmod())
if __name__ == "__main__":
main()