-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.py
382 lines (296 loc) · 10.1 KB
/
process.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/env python
"""
Advent of Code 2020: Day 11
"""
import sys
from collections import Counter
from copy import deepcopy
from pathlib import Path
import os
from typing import List
TXT_FILES = [Path(f) for f in os.listdir('./') if '.txt' in f]
def extend(layout: List[List[str]]) -> List[List[str]]:
"""
Extend layout by adding a border of empty seats
:param layout: initial layout
:return: layout with a border of empty seats
"""
empty_row = ['.'] * (2 + len(layout[0]))
retval = list()
retval.append(empty_row)
for r in layout:
r.insert(0, '.')
r.insert(len(r), '.')
retval.append(r)
retval.append(empty_row)
return retval
def count_occupied_adjacent_seats(
layout: List[List[str]], row=int, col=int) -> int:
"""
Return the quantity of occupied adjacent seats
:param layout: seat layout
:param row: row index of the seat of interest
:param col: column index of the seat of interest
:return: quantity of occupied seats adjacent to the seat of interest
"""
top_row = layout[row-1][col-1:col+2]
ctr_row = [layout[row][col-1], layout[row][col+1]]
bot_row = layout[row+1][col-1:col+2]
adj_seats = top_row + ctr_row + bot_row
occupied_adjacent_seats = sum(1 for s in adj_seats if s == '#')
return occupied_adjacent_seats
def count_occupied_seats(layout: List[List[str]]) -> int:
"""
Count quantity of occupied seats in a given layout
:param layout: seat layout
:return: quantity of occupied seats
"""
occupied_seats = sum(sum(1 for seat in r if seat == '#') for r in layout)
return occupied_seats
def compute_occupied_layout(layout: List[List[str]]) -> List[List[str]]:
"""
Compute ouccpied seat map
:param layout: seat layout
:return: layout with number of occupied adjacent seats or '.'
"""
depth = len(layout)
width = len(layout[0])
occupied_layout = list()
occupied_layout.append(['.'] * width)
for r in range(1, depth - 1):
row = ['.']
for c in range(1, width - 1):
if layout[r][c] == '.':
row.append('.')
else:
row.append(count_occupied_adjacent_seats(
layout=layout, row=r, col=c))
row.append('.')
occupied_layout.append(row)
occupied_layout.append(['.'] * width)
return occupied_layout
def compute_round(layout: List[List[str]]) -> List[List[str]]:
"""
Apply round of rules defined in part 1
:param layout: seat layout
:return: layout following a round
"""
updated_layout = deepcopy(layout)
occupied_layout = compute_occupied_layout(updated_layout)
depth = len(updated_layout)
width = len(updated_layout[0])
for r in range(1, depth - 1):
for c in range(1, width - 1):
if updated_layout[r][c] != 'L':
continue
elif occupied_layout[r][c] == 0:
updated_layout[r][c] = '#'
occupied_updated_layout = compute_occupied_layout(updated_layout)
depth = len(updated_layout)
width = len(updated_layout[0])
for r in range(1, depth - 1):
for c in range(1, width - 1):
if updated_layout[r][c] != '#':
continue
elif occupied_updated_layout[r][c] >= 4:
updated_layout[r][c] = 'L'
return updated_layout
def count_occupied_visible_seats(
layout: List[List[str]], row=int, col=int) -> int:
"""
Return the quantity of occupied visible seats
:param layout: seat layout
:param row: row index of the seat of interest
:param col: column index of the seat of interest
:return: quantity of occupied seats adjacent to the seat of interest
"""
inside = lambda r_, c_: \
(0 <= r_ < len(layout)) and (0 <= c_ < len(layout[0]))
# right ray
dist = 1
path_list = {(45 * path):None for path in range(1, 9)}
while not all(path_list.values()):
# North
if not path_list[360]:
r = row - dist
c = col
if not inside(r, c):
path_list[360] = 'B' # border
else:
seat = layout[r][c]
if seat in ['#', 'L']:
path_list[360] = seat
# North-East
if not path_list[45]:
r = row - dist
c = col + dist
if not inside(r, c):
path_list[45] = 'B' # border
else:
seat = layout[r][c]
if seat in ['#', 'L']:
path_list[45] = seat
# East
if not path_list[90]:
r = row
c = col + dist
if not inside(r, c):
path_list[90] = 'B' # border
else:
seat = layout[r][c]
if seat in ['#', 'L']:
path_list[90] = seat
# South-East
if not path_list[135]:
r = row + dist
c = col + dist
if not inside(r, c):
path_list[135] = 'B' # border
else:
seat = layout[r][c]
if seat in ['#', 'L']:
path_list[135] = seat
# South
if not path_list[180]:
r = row + dist
c = col
if not inside(r, c):
path_list[180] = 'B' # border
else:
seat = layout[r][c]
if seat in ['#', 'L']:
path_list[180] = seat
# South-West
if not path_list[225]:
r = row + dist
c = col - dist
if not inside(r, c):
path_list[225] = 'B' # border
else:
seat = layout[r][c]
if seat in ['#', 'L']:
path_list[225] = seat
# West
if not path_list[270]:
r = row
c = col - dist
if not inside(r, c):
path_list[270] = 'B' # border
else:
seat = layout[r][c]
if seat in ['#', 'L']:
path_list[270] = seat
# North-West
if not path_list[315]:
r = row - dist
c = col - dist
if not inside(r, c):
path_list[315] = 'B' # border
else:
seat = layout[r][c]
if seat in ['#', 'L']:
path_list[315] = seat
dist += 1
occupied_visible_seats = sum(1 for p in path_list.values() if p == '#')
return occupied_visible_seats
def compute_visible_occupied_layout(layout: List[List[str]]) -> List[List[str]]:
"""
Compute occupied seat map for part 2
:param layout: seat layout
:return: layout with number of occupied adjacent seats or '.'
"""
depth = len(layout)
width = len(layout[0])
occupied_layout = list()
occupied_layout.append(['.'] * width)
for r in range(1, depth - 1):
row = ['.']
for c in range(1, width - 1):
if layout[r][c] == '.':
row.append('.')
else:
row.append(count_occupied_visible_seats(
layout=layout, row=r, col=c))
row.append('.')
occupied_layout.append(row)
occupied_layout.append(['.'] * width)
return occupied_layout
def compute_round_part2(layout: List[List[str]]) -> List[List[str]]:
"""
Apply round of rules defined in part 2
:param layout: seat layout
:return: layout following a round
"""
updated_layout = deepcopy(layout)
occupied_layout = compute_visible_occupied_layout(updated_layout)
depth = len(updated_layout)
width = len(updated_layout[0])
for r in range(1, depth - 1):
for c in range(1, width - 1):
if updated_layout[r][c] != 'L':
continue
elif occupied_layout[r][c] == 0:
updated_layout[r][c] = '#'
occupied_updated_layout = compute_visible_occupied_layout(updated_layout)
depth = len(updated_layout)
width = len(updated_layout[0])
for r in range(1, depth - 1):
for c in range(1, width - 1):
if updated_layout[r][c] != '#':
continue
elif occupied_updated_layout[r][c] >= 5:
updated_layout[r][c] = 'L'
return updated_layout
def process(file: Path) -> int:
"""
Process input file yielding the submission value
:param file: file containing the input values
:return: value to submit
"""
rounds = 0
updated_layout = extend(layout=[list(l.strip()) for l in open(file)])
layout = [['-' for s in row] for row in updated_layout]
while updated_layout != layout:
rounds += 1
layout = updated_layout
updated_layout = compute_round(layout=layout)
print(f'Converged after {rounds} rounds')
occupied_seats = count_occupied_seats(layout=updated_layout)
submission = occupied_seats
return submission
def process_part2(file: Path) -> int:
"""
Process input file yielding the submission value
:param file: file containing the input values
:return: value to submit
"""
rounds = 0
updated_layout = extend(layout=[list(l.strip()) for l in open(file)])
layout = [['-' for s in row] for row in updated_layout]
while updated_layout != layout:
rounds += 1
layout = updated_layout
updated_layout = compute_round_part2(layout=layout)
print(f'Converged after {rounds} rounds')
occupied_seats = count_occupied_seats(layout=updated_layout)
submission = occupied_seats
return submission
def main() -> int:
"""
Main function
:return: Shell exit code
"""
if False:
layout__ = extend(layout=[list(l.strip()) for l in open('./part2_sample1.txt')])
count_occupied_visible_seats(layout__, 5, 4)
return 0
for file in TXT_FILES:
submission = process(file=Path(file))
print(f'In file {file}, submission: {submission}')
print('Part 2')
for file in TXT_FILES:
submission = process_part2(file=Path(file))
print(f'In file {file}, submission: {submission}')
return 0
if __name__ == '__main__':
sys.exit(main())