-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11_part1.py
36 lines (27 loc) · 1.05 KB
/
day11_part1.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
with open('input11.txt', 'r') as file:
puzzle = [list(line) for line in file.read().strip().splitlines()]
def run(ref):
changed = False
for x in range(len(ref)):
for y in range(len(ref[x])):
if ref[x][y] in ('#', 'L'):
neighbors = 0
for offset_x in range(-1, 2):
for offset_y in range(-1, 2):
if (offset_x or offset_y) and x + offset_x >= 0 and x + offset_x < len(ref) and y + offset_y >= 0 and y + offset_y < len(ref[x]) and ref[x + offset_x][y + offset_y] == '#':
neighbors += 1
if ref[x][y] == 'L' and neighbors == 0:
puzzle[x][y] = '#'
changed = True
elif ref[x][y] == '#' and neighbors >= 4:
puzzle[x][y] = 'L'
changed = True
return changed
while run([row[:] for row in puzzle]):
pass
occupied = 0
for row in puzzle:
for seat in row:
if seat == '#':
occupied += 1
print(occupied)