-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.py
45 lines (30 loc) · 965 Bytes
/
solution.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import math
def get_view_slices(grid, tree_x, tree_y):
row = grid[tree_y]
col = [n[tree_x] for n in grid]
return [
row[0:tree_x][::-1],
row[tree_x+1:],
col[0:tree_y][::-1],
col[tree_y + 1:],
]
def dist_to_first(view, tree_height):
return next(
(dist + 1 for dist, height in enumerate(view) if height >= tree_height),
len(view),
)
def sees_edge(view, tree_height):
return all(v < tree_height for v in view)
input = open("input.txt").read()
rows = [list(row) for row in input.splitlines()]
scores = []
visible_count = 0
for y, rows in enumerate(input):
for x, tree_height in enumerate(rows):
slices = get_view_slices(input, x, y)
dists = [dist_to_first(s, tree_height) for s in slices]
scores.append(math.prod(dists))
if any(sees_edge(s, tree_height) for s in slices):
visible_count += 1
print(visible_count)
print(max(scores))