-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.py
66 lines (59 loc) · 1.58 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
57
58
59
60
61
62
63
64
65
66
import aoc
data = aoc.strlist(11)
h = len(data)
w = len(data[0])
p = {}
q = {}
for i in range(w):
for j in range(h):
if data[j][i] == ".": continue
p[i, j] = data[j][i]
q[i, j] = data[j][i]
d = {}
for i in range(w):
for j in range(h):
d[i, j] = []
for u, v in ((1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1)):
if (i + u, j + v) in p:
d[i, j].append((i + u, j + v))
c = set(p.keys())
while c:
pp = dict(p)
cc = set()
for i, j in c:
n = [p[k] for k in d[i, j]].count("#")
if p[i, j] == "L" and n == 0:
pp[i, j] = "#"
cc.update(d[i, j])
elif p[i, j] == "#" and n >= 4:
pp[i, j] = "L"
cc.update(d[i, j])
p = pp
c = cc
d = {}
for i in range(w):
for j in range(h):
d[i, j] = []
for u, v in ((1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1)):
r = 1
while (0 <= i + r*u < w) and (0 <= j + r*v < h):
if (i + r*u, j + r*v) in q:
d[i, j].append((i + r*u, j + r*v))
break
r += 1
c = set(q.keys())
while c:
qq = dict(q)
cc = set()
for i, j in c:
n = [q[k] for k in d[i, j]].count("#")
if q[i, j] == "L" and n == 0:
qq[i, j] = "#"
cc.update(d[i, j])
elif q[i, j] == "#" and n >= 5:
qq[i, j] = "L"
cc.update(d[i, j])
q = qq
c = cc
print(list(p.values()).count("#"), list(q.values()).count("#"))
aoc.tock()