44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
import numpy as np
|
|
|
|
forest = np.array([np.array(list(map(int, x.strip()))) for x in open("input.txt")])
|
|
|
|
print("x", forest)
|
|
|
|
visible = np.full_like(forest, False, dtype=bool)
|
|
|
|
# top
|
|
height = np.repeat(-1, len(forest[0]))
|
|
for i in range(0, len(forest)):
|
|
visible[i] = visible[i] | (forest[i] > height)
|
|
height = np.max([height, forest[i]], axis=0)
|
|
if np.all(height == 9):
|
|
break
|
|
|
|
# bottom
|
|
height = np.repeat(-1, len(forest[0]))
|
|
for i in range(len(forest)-1, -1, -1):
|
|
visible[i] = visible[i] | (forest[i] > height)
|
|
height = np.max([height, forest[i]], axis=0)
|
|
if np.all(height == 9):
|
|
break
|
|
|
|
# left
|
|
height = np.repeat(-1, len(forest))
|
|
for i in range(0, len(forest[0])):
|
|
visible[:, i] = visible[:, i] | (forest[:, i] > height)
|
|
height = np.max([height, forest[:, i]], axis=0)
|
|
if np.all(height == 9):
|
|
break
|
|
|
|
# left
|
|
height = np.repeat(-1, len(forest))
|
|
for i in range(len(forest[0])-1, -1, -1):
|
|
visible[:, i] = visible[:, i] | (forest[:, i] > height)
|
|
height = np.max([height, forest[:, i]], axis=0)
|
|
if np.all(height == 9):
|
|
break
|
|
|
|
print(np.count_nonzero(visible))
|
|
|