-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.py
62 lines (53 loc) · 1.65 KB
/
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
from lib import *
input = read_input(2020, 11)
lines = input.splitlines()
graph = {}
active = set()
for y, line in enumerate(lines):
for x, c in enumerate(line):
if c == ".":
continue
graph[(x, y)] = []
for dy in range(-1, 2):
for dx in range(-1, 2):
if dx == 0 == dy:
continue
if 0 <= (i := y + dy) < len(lines) and 0 <= (j := x + dx) < len(line) and lines[i][j] != ".":
graph[(x, y)].append((j, i))
while True:
new_active = set()
for p, qs in graph.items():
cnt = sum(q in active for q in qs)
if p not in active and not cnt or p in active and cnt < 4:
new_active.add(p)
if active == new_active:
break
active = new_active
print(len(active))
graph = {}
active = set()
for y, line in enumerate(lines):
for x, c in enumerate(line):
if c == ".":
continue
graph[(x, y)] = []
for dy in range(-1, 2):
for dx in range(-1, 2):
if dx == 0 == dy:
continue
k = 1
while 0 <= (i := y + k * dy) < len(lines) and 0 <= (j := x + k * dx) < len(line):
if lines[i][j] != ".":
graph[(x, y)].append((j, i))
break
k += 1
while True:
new_active = set()
for p, qs in graph.items():
cnt = sum(q in active for q in qs)
if p not in active and not cnt or p in active and cnt < 5:
new_active.add(p)
if active == new_active:
break
active = new_active
print(len(active))