-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.py
46 lines (31 loc) · 1.13 KB
/
day08.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
46
from aoc import *
from math import prod
def viewing_distance(height, d):
return next((i for i, h in enumerate(d, 1) if h >= height), inf)
def solve(file=8):
horizontal = mapl(digits, read_input(file))
vertical = transpose(horizontal)
size = len(horizontal)
visible_from_outside = 0
highest_score = 0
for y in range(size):
for x in range(size):
height = horizontal[y][x]
row = horizontal[y]
col = vertical[x]
directions = (
row[:x][::-1],
row[x+1:],
col[:y][::-1],
col[y+1:]
)
viewing_distances = [viewing_distance(height, d) for d in directions]
visible_from_outside += any(dist == inf for dist in viewing_distances)
visible_trees = (min(len(d), v)
for v, d in zip(viewing_distances, directions))
highest_score = max(highest_score, prod(visible_trees))
return visible_from_outside, highest_score
print(solve())
def test():
assert solve("08_test") == (21, 8)
assert solve() == (1829, 291840)