1
|
|
|
import functools |
2
|
|
|
import operator |
3
|
|
|
import pathlib |
4
|
|
|
|
5
|
|
|
BOARD_SIZE = 99 |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
def is_visible(board, x, y): |
9
|
|
|
return any( |
10
|
|
|
board[y][x] > max(trees) |
11
|
|
|
for trees in get_neighbours_tree(board, x, y) |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
def count_visible(board): |
16
|
|
|
return sum( |
17
|
|
|
is_visible(board, x, y) |
18
|
|
|
for x in range(1, BOARD_SIZE - 1) |
19
|
|
|
for y in range(1, BOARD_SIZE - 1) |
20
|
|
|
) + (BOARD_SIZE - 1) * 4 |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
def get_neighbours_tree(board, x, y): |
24
|
|
|
return ( |
25
|
|
|
board[y][:x][::-1], |
26
|
|
|
board[y][x + 1:], |
27
|
|
|
tuple(line[x] for line in board[:y])[::-1], |
28
|
|
|
tuple(line[x] for line in board[y + 1:]) |
29
|
|
|
) |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
def get_first_non_visible_from(h, trees): |
33
|
|
|
for idx, tree in enumerate(trees, start=1): |
34
|
|
|
if tree >= h: |
35
|
|
|
return idx |
36
|
|
|
return len(trees) |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
def scenic_core(board, x, y): |
40
|
|
|
return functools.reduce( |
41
|
|
|
operator.mul, |
42
|
|
|
( |
43
|
|
|
get_first_non_visible_from(board[y][x], trees) |
|
|
|
|
44
|
|
|
for trees in get_neighbours_tree(board, x, y) |
45
|
|
|
) |
46
|
|
|
) |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
def get_highest_scenic_score(board): |
50
|
|
|
return max( |
51
|
|
|
scenic_core(board, x, y) |
52
|
|
|
for x in range(BOARD_SIZE) |
53
|
|
|
for y in range(BOARD_SIZE) |
54
|
|
|
) |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
def main(): |
58
|
|
|
content = pathlib.Path('./input.txt').read_text() |
59
|
|
|
board = [line for line in content.splitlines()] |
60
|
|
|
|
61
|
|
|
print('Part 1:', count_visible(board)) |
62
|
|
|
print('Part 2:', get_highest_scenic_score(board)) |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
if __name__ == '__main__': |
66
|
|
|
main() |
67
|
|
|
|