-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_11.py
80 lines (60 loc) · 1.7 KB
/
day_11.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
import input_11
from aoc import advent_of_code
FLASH_ENERGY_LEVEL = 9
def neighbouring_cells(x, y, x_height, y_height):
neighbours = [
[x, y + 1],
[x - 1, y + 1],
[x + 1, y + 1],
[x - 1, y],
[x + 1, y],
[x, y - 1],
[x - 1, y - 1],
[x + 1, y - 1],
]
def in_bounds(cord):
x, y = cord
return y >= 0 and x >= 0 and y < y_height and x < x_height
return filter(in_bounds, neighbours)
def increment(x, y, input, flashed_this_round):
input[y][x] += 1
if input[y][x] > FLASH_ENERGY_LEVEL and [x, y] not in flashed_this_round:
flashed_this_round.append([x, y])
for x, y in neighbouring_cells(x, y, len(input[y]), len(input)):
increment(x, y, input, flashed_this_round)
def increment_grid(input):
flashed_this_round = []
for y in range(len(input)):
for x in range(len(input[y])):
increment(x, y, input, flashed_this_round)
for x, y in flashed_this_round:
input[y][x] = 0
return len(flashed_this_round)
def part_one(input):
return sum([increment_grid(input) for _ in range(1, 101)])
def part_two(input):
for step in range(1, 400):
increment_grid(input)
if all(all(energy == 0 for energy in line) for line in input):
return step
return 0
advent_of_code(
{
"day": 11,
"part": 1,
"fn": part_one,
"sample": input_11.sample(),
"expected": 1656,
"real": input_11.real(),
}
)
advent_of_code(
{
"day": 11,
"part": 2,
"fn": part_two,
"sample": input_11.sample(),
"expected": 195,
"real": input_11.real(),
}
)