Files
python-aoc-2022/day08/part1.py
Sebastian Seedorf 60dc09699f Day 08
2022-12-09 09:26:32 +01:00

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))