-
Notifications
You must be signed in to change notification settings - Fork 0
/
12_11.py
208 lines (188 loc) · 12.1 KB
/
12_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import sys
inp = """
LLLLLLLLL.L.LLL.L.LLLLL.LLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLL.LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLL.LLLLL.LLLLLLLLLLLL.LLLLLL.LLLLL.LLLLLLL.LLLLLLLL
LLLLLLLLL.LLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLL.LLLLLL.LLLLL.LLLLLLLLLLLLLLLL
LLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLLLLLLLL.LLLLLL.LLLLL.LLLLLLL.LLLLLLLL
LLLLLLLLLLLLLLL.LLLLLLL.LLL.LLLLLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLL
L..LL.L.L....L..L.L....L.....L.L.L......L..L..L....L.LL.......L.....L....L..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LLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLL.LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLL.LLLLLL.LLLLL.LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLL.LLLLLLL.LLLL..LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL
LLLLLLLLL.LLLLL.LLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLL.LLL.L.LLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLLLLL"""
grid = {}
rows = inp.split("\n")
for rix, row in enumerate(rows):
for cix, val in enumerate(row):
grid[(rix, cix)] = val
def adjacent(grid, x, y):
seats = [
grid.get((x + 1, y), None),
grid.get((x - 1, y), None),
grid.get((x + 1, y + 1), None),
grid.get((x, y + 1), None),
grid.get((x - 1, y + 1), None),
grid.get((x + 1, y - 1), None),
grid.get((x, y - 1), None),
grid.get((x - 1, y - 1), None),
]
return [s for s in seats if s is not None]
def first_seat(grid, x, y, dx, dy):
x += dx
y += dy
s = grid.get((x, y), None)
while s is not None:
if s != ".":
return s
x += dx
y += dy
s = grid.get((x, y), None)
return None
def visible(grid, x, y):
seats = [
first_seat(grid, x, y, 1, 0),
first_seat(grid, x, y, -1, 0),
first_seat(grid, x, y, 1, 1),
first_seat(grid, x, y, 0, 1),
first_seat(grid, x, y, -1, 1),
first_seat(grid, x, y, 1, -1),
first_seat(grid, x, y, 0, -1),
first_seat(grid, x, y, -1, -1),
]
return [s for s in seats if s is not None]
def step(grid):
new = {}
changed = False
for (x, y), val in grid.items():
adj = adjacent(grid, x, y)
occ = len([s for s in adj if s == "#"])
if val == "L" and occ == 0:
new[(x, y)] = "#"
changed = True
elif val == "#" and occ >= 4:
new[(x, y)] = "L"
changed = True
else:
new[(x, y)] = val
return (new, changed)
def step2(grid):
new = {}
changed = False
for (x, y), val in grid.items():
adj = visible(grid, x, y)
occ = len([s for s in adj if s == "#"])
if val == "L" and occ == 0:
new[(x, y)] = "#"
changed = True
elif val == "#" and occ >= 5:
new[(x, y)] = "L"
changed = True
else:
new[(x, y)] = val
return (new, changed)
def print_grid_dict(grid, charmap=None, default=0):
# Expects a grid of (x, y) -> val
# Charmap maps vals to tokens for printing. Otherwise prints str(val).
min_x = min([x for x, y in grid.keys()])
max_x = max([x for x, y in grid.keys()])
min_y = min([y for x, y in grid.keys()])
max_y = max([y for x, y in grid.keys()])
for y in range(min_y, max_y + 1):
for x in range(min_x, max_x + 1):
v = grid.get((x, y), default)
if charmap:
v = charmap[v]
sys.stdout.write(str(v))
sys.stdout.write("\n")
changed = True
grid, changed = step2(grid)
while changed:
grid, changed = step2(grid)
print(len([s for s in grid.values() if s == "#"]))