51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
import numpy as np
|
|
from day11.common import char_to_int
|
|
|
|
lines = np.array([[char_to_int(x) for x in line.strip()] for line in open("input.txt")], dtype=np.byte)
|
|
shape = lines.shape
|
|
|
|
RAYS = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
|
|
|
|
last = -1
|
|
while True:
|
|
curr = np.count_nonzero(lines == 1)
|
|
if curr == last:
|
|
print(last)
|
|
break
|
|
last = curr
|
|
|
|
new = np.zeros_like(lines, dtype=np.byte)
|
|
for idx, val in np.ndenumerate(lines):
|
|
if val == 0:
|
|
continue
|
|
if val == -1:
|
|
found = False
|
|
for ray in RAYS:
|
|
ray = np.array(ray)
|
|
pos = np.array(idx) + ray
|
|
while 0 <= pos[0] < shape[0] and 0 <= pos[1] < shape[1]:
|
|
if lines[tuple(pos)] == 1:
|
|
found = True
|
|
break
|
|
elif lines[tuple(pos)] == -1:
|
|
break
|
|
pos = pos + ray
|
|
if found:
|
|
break
|
|
new[idx] = 1 if not found else -1
|
|
continue
|
|
if val == 1:
|
|
found = 0
|
|
for ray in RAYS:
|
|
ray = np.array(ray)
|
|
pos = np.array(idx) - ray
|
|
while 0 <= pos[0] < shape[0] and 0 <= pos[1] < shape[1]:
|
|
if lines[tuple(pos)] == 1:
|
|
found += 1
|
|
break
|
|
elif lines[tuple(pos)] == -1:
|
|
break
|
|
pos = pos - ray
|
|
new[idx] = -1 if found >= 5 else 1
|
|
lines = new
|