-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.py
56 lines (48 loc) · 1.28 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
from utils import int_grid, read_day
start_octos = read_day(11, int_grid)
size = len(start_octos)
def part1():
octos = [r.copy() for r in start_octos]
count = 0
for i in range(100):
for i, row in enumerate(octos):
for j, octo in enumerate(row):
octos[i][j] += 1
count += flash(octos)
return count
def flash(octos):
flashed = set()
while True:
found = False
for i, row in enumerate(octos):
for j, octo in enumerate(row):
o = octos[i][j]
if (i, j) not in flashed and o > 9:
found = True
flashed.add((i, j))
for i2, j2 in neighbors(i, j, size):
octos[i2][j2] += 1
if not found:
break
for i, j in flashed:
octos[i][j] = 0
return len(flashed)
def neighbors(x, y, limit):
out = []
for xn in range(max(0, x - 1), min(limit, x + 2)):
for yn in range(max(0, y - 1), min(limit, y + 2)):
if xn == x and yn == y:
continue
out.append((xn, yn))
return out
print(f'Part 1: {part1()}')
def part2():
octos = [r.copy() for r in start_octos]
for step in range(1000000):
for i, row in enumerate(octos):
for j, octo in enumerate(row):
octos[i][j] += 1
if flash(octos) == 100:
return step + 1
return None
print(f'Part 2: {part2()}')