-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc11.py
executable file
·68 lines (54 loc) · 1.85 KB
/
aoc11.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
#!/usr/bin/env python3
import utils
class CaveMap:
def __init__(self, data):
self.data = data.values
self.width = data.width
self.height = data.height
def __str__(self):
return "\n".join("".join(str(i) if i <= 9 else "X" for i in line) for line in self.data) + "\n"
def check_flash(self, flashed, x, y):
flash_count = 0
if self.data[y][x] > 9 and (x, y) not in flashed:
flash_count += 1
flashed.add((x, y))
for x2, y2 in self.sibling_coords(x, y):
self.data[y2][x2] += 1
flash_count += self.check_flash(flashed, x2, y2)
return flash_count
def sibling_coords(self, x, y):
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
if dx == 0 and dy == 0:
continue
if x + dx < 0 or x + dx >= self.width:
continue
if y + dy < 0 or y + dy >= self.height:
continue
yield x + dx, y + dy
def step(self):
for y in range(0, self.height):
for x in range(0, self.width):
self.data[y][x] += 1
flash_count = 0
flashed = set()
for y in range(0, self.height):
for x in range(0, self.width):
flash_count += self.check_flash(flashed, x, y)
if all(v > 9 for line in self.data for v in line):
return True
for y in range(0, self.height):
for x in range(0, self.width):
if self.data[y][x] > 9:
self.data[y][x] = 0
return False
def main():
cm = CaveMap(utils.read_matrix(utils.input()))
print(cm)
for i in range(10000000):
print(cm)
if cm.step():
print(i + 1)
return
if __name__ == "__main__":
main()