-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday11.py
More file actions
72 lines (51 loc) · 1.35 KB
/
day11.py
File metadata and controls
72 lines (51 loc) · 1.35 KB
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
def parse(input):
ret = {}
for y, row in enumerate(input.strip().splitlines()):
for x, char in enumerate(row):
ret[(x, y)] = int(char)
return ret
def neighbors(p):
for x in range(p[0] - 1, p[0] + 2):
for y in range(p[1] - 1, p[1] + 2):
if (x, y) == p:
continue
yield (x, y)
def add_energy(p, input, flashed):
if p in flashed:
return
input[p] += 1
if input[p] <= 9:
return
input[p] = 0
flashed.add(p)
for n in neighbors(p):
if n not in input:
continue
add_energy(n, input, flashed)
def part1(input, steps):
input = parse(input)
flashes = 0
for step in range(steps):
flashed = set()
for p in input:
add_energy(p, input, flashed)
flashes += len(flashed)
return flashes
def part2(input):
input = parse(input)
step = 0
while True:
step += 1
flashed = set()
for p in input:
add_energy(p, input, flashed)
if len(flashed) == len(input):
return step
if __name__ == '__main__':
import sys
import utils
input = sys.stdin.read()
p1, t1 = utils.time(part1, input, 100)
print(f'part1: {p1} ({t1:.20f})')
p2, t2 = utils.time(part2, input)
print(f'part2: {p2} ({t2:.20f})')