-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08_treetop_tree_house.py
71 lines (58 loc) · 1.86 KB
/
day08_treetop_tree_house.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
def main(patch: str) -> None:
nb_visible = 0
score = 0
for y, line in enumerate(patch.splitlines()):
for x, _ in enumerate(line):
nb_visible += visible(x, y, patch.splitlines())
score = max(scenic_score(x, y, patch.splitlines()), score)
print("Part One:")
print(nb_visible)
print("Part Two:")
print(score)
def visible(x: int, y: int, patch: list[str]) -> bool:
line = patch[y]
if x in (0, len(line) - 1) or y in (0, len(patch) - 1):
return True
north = max(int(line[x]) for line in patch[:y])
east = max(int(tree) for tree in line[x + 1 :])
south = max(int(line[x]) for line in patch[y + 1 :])
west = max(int(tree) for tree in line[:x])
tree = int(line[x])
return north < tree or east < tree or west < tree or south < tree
def scenic_score(x: int, y: int, patch: list[str]) -> int:
height = int(patch[y][x])
north_view = (
viewing_distance(height, [int(line[x]) for line in patch[y - 1 :: -1]])
if y > 0
else 0
)
east_view = (
viewing_distance(height, [int(tree) for tree in patch[y][x + 1 :]])
if x < len(patch[y])
else 0
)
south_view = (
viewing_distance(height, [int(line[x]) for line in patch[y + 1 :]])
if y < len(patch)
else 0
)
west_view = (
viewing_distance(height, [int(tree) for tree in patch[y][x - 1 :: -1]])
if x > 0
else 0
)
return north_view * east_view * south_view * west_view
def viewing_distance(height: int, trees: list[int]) -> int:
for i, tree in enumerate(trees):
if tree >= height:
return i + 1
return len(trees)
if __name__ == "__main__":
INPUT = """30373
25512
65332
33549
35390
"""
main(INPUT)