Passed
Push — main ( 8ecbea...c5b7e2 )
by Yohann
01:46 queued 51s
created

main.scenic_core()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
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)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable trees does not seem to be defined.
Loading history...
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