-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11-2.py
60 lines (51 loc) · 1.36 KB
/
day11-2.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
import copy
seats = []
with open("day11-1.txt") as f:
for line in f:
seats.append(list(line.strip()))
visible = {}
w = len(seats[0])
h = len(seats)
def first_nonempty(origin, direction):
o_x, o_y = origin
d_x, d_y = direction
p_x = o_x + d_x
p_y = o_y + d_y
if p_x < 0 or p_y < 0 or p_x >= w or p_y >= h:
return False
while seats[p_y][p_x] == ".":
p_x += d_x
p_y += d_y
if p_x < 0 or p_y < 0 or p_x >= w or p_y >= h:
return False
return (p_x, p_y)
# build surrounding list
for y in range(h):
for x in range(w):
visible[(x, y)] = []
for d in [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)]:
if (l := first_nonempty((x, y), d)):
visible[(x, y)].append(l)
def get_visible(s, x, y):
l = []
for (i, j) in visible[(x, y)]:
l.append(s[j][i])
return l
def iteration(s):
n = copy.deepcopy(s)
for y in range(h):
for x in range(w):
surround = get_visible(s, x, y)
if s[y][x] == "L" and (not "#" in surround):
n[y][x] = "#"
elif s[y][x] == "#" and surround.count("#") >= 5:
n[y][x] = "L"
return n
n = iteration(seats)
while n != seats:
seats = n.copy()
n = iteration(seats)
tot = 0
for i in n:
tot += i.count("#")
print(tot)