-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.py
112 lines (90 loc) · 2.65 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
# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
import os
import itertools
top_dir = os.path.dirname(os.path.abspath(__file__)) + "/../../"
def get_grid_from_lines(lines):
for x, l in enumerate(lines):
for y, v in enumerate(l.strip()):
yield (x, y), int(v)
def get_grid_from_file(file_path=top_dir + "resources/year2021_day11_input.txt"):
with open(file_path) as f:
return dict(get_grid_from_lines(f))
def show_grid(grid):
x_vals = [x for x, _ in grid.keys()]
y_vals = [y for _, y in grid.keys()]
x_range = list(range(min(x_vals), 1 + max(x_vals)))
y_range = list(range(min(y_vals), 1 + max(y_vals)))
for x in x_range:
print("".join(str(grid.get((x, y), "?")) for y in y_range))
def neighbours(p):
x, y = p
for dx, dy in itertools.product([-1, 0, 1], repeat=2):
if (dx, dy) != (0, 0):
yield x + dx, y + dy
def next_step(grid):
grid2 = {p: v + 1 for p, v in grid.items()}
to_flash = set(p for p, v in grid2.items() if v > 9)
flashed = set()
while to_flash:
p = to_flash.pop()
if p not in flashed:
flashed.add(p)
for n in neighbours(p):
if n in grid2:
grid2[n] += 1
if grid2[n] > 9:
to_flash.add(n)
for p in flashed:
grid2[p] = 0
return grid2
def nb_flashes(grid, n):
nb_flash = 0
for _ in range(n):
grid = next_step(grid)
nb_flash += sum(v == 0 for v in grid.values())
return nb_flash
def first_synchro(grid):
for i in itertools.count():
if all(v == 0 for v in grid.values()):
return i
grid = next_step(grid)
def run_tests():
grid = [
"11111",
"19991",
"19191",
"19991",
"11111",
]
grid = dict(get_grid_from_lines(grid))
# grid1 = next_step(grid)
# grid2 = next_step(grid1)
# show_grid(grid2)
assert nb_flashes(grid, 2) == 9
grid = [
"5483143223",
"2745854711",
"5264556173",
"6141336146",
"6357385478",
"4167524645",
"2176841721",
"6882881134",
"4846848554",
"5283751526",
]
grid = dict(get_grid_from_lines(grid))
assert nb_flashes(grid, 10) == 204
assert nb_flashes(grid, 100) == 1656
assert first_synchro(grid) == 195
def get_solutions():
grid = get_grid_from_file()
print(nb_flashes(grid, 100) == 1717)
print(first_synchro(grid) == 476)
if __name__ == "__main__":
begin = datetime.datetime.now()
run_tests()
get_solutions()
end = datetime.datetime.now()
print(end - begin)