-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8-2.py
More file actions
30 lines (24 loc) · 775 Bytes
/
8-2.py
File metadata and controls
30 lines (24 loc) · 775 Bytes
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
# python 8-2.py input8.txt
import numpy as np
import sys
def seen_trees(trees, tree):
blocking = trees >= tree
if blocking.any():
# argmax -> returns index of first occurrence of maximum
return np.argmax(blocking) + 1
else:
return len(trees)
trees = np.genfromtxt(sys.argv[1], delimiter=1, dtype=np.uint8)
height, width = trees.shape
max_score = 0
for i in range(1, height - 1):
for j in range(1, width - 1):
tree = trees[i, j]
score = (
seen_trees(trees[i, j + 1 :], tree)
* seen_trees(np.flip(trees[i, :j]), tree)
* seen_trees(trees[i + 1 :, j], tree)
* seen_trees(np.flip(trees[:i, j]), tree)
)
max_score = max(max_score, score)
print(max_score)