43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
import numpy as np
|
|
from day11.common import char_to_int
|
|
|
|
seats = np.array([[char_to_int(x) for x in line.strip()] for line in open("input.txt")], dtype=np.byte)
|
|
RAYS = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
|
|
occupied = -1
|
|
|
|
|
|
def find_neighbor(idx, ray):
|
|
idx = tuple(map(sum, zip(idx, ray)))
|
|
while 0 <= idx[0] < seats.shape[0] and 0 <= idx[1] < seats.shape[1]:
|
|
if seats[idx] == -1:
|
|
return 0
|
|
elif seats[idx] == 0:
|
|
idx = tuple(map(sum, zip(idx, ray)))
|
|
continue
|
|
else:
|
|
return 1
|
|
else:
|
|
return 0
|
|
|
|
|
|
while True:
|
|
new_occupied = np.count_nonzero(seats == 1)
|
|
if occupied == new_occupied:
|
|
break
|
|
occupied = new_occupied
|
|
result = np.zeros_like(seats)
|
|
for idx, val in np.ndenumerate(seats):
|
|
if val == 0:
|
|
continue
|
|
neighbors = sum(find_neighbor(idx, ray) for ray in RAYS)
|
|
if val == -1 and neighbors == 0:
|
|
result[idx] = 1
|
|
elif val == 1 and neighbors >= 5:
|
|
result[idx] = -1
|
|
else:
|
|
result[idx] = seats[idx]
|
|
seats = result
|
|
print(occupied)
|
|
|
|
print(occupied)
|