summaryrefslogtreecommitdiffstats
path: root/8.py
blob: 152517c43f13ae86ecbb47c0e13ddfada95e5771 (plain)
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
from functools import reduce

with open('8.in') as f:
    grid = [[int(c) for c in line.rstrip()] for line in f]

visible = set()

for y, row in enumerate(grid):
    height = -1
    for x, t in enumerate(row):
        if t > height:
            visible.add((x, y))
            height = t
    height = -1
    for x, t in reversed(list(enumerate(row))):
        if t > height:
            visible.add((x, y))
            height = t

for x in range(len(grid[0])):
    height = -1
    for y, row in enumerate(grid):
        t = row[x]
        if t > height:
            visible.add((x, y))
            height = t
    height = -1
    for y, row in reversed(list(enumerate(grid))):
        t = row[x]
        if t > height:
            visible.add((x, y))
            height = t

print(len(visible))

def calc_scenic(grid, x, y):
    height = grid[y][x]
    scores = [0 for _ in range(4)]
    for nx in range(x+1, len(grid[y])):
        # print('right', nx, y)
        scores[0] += 1
        if grid[y][nx] >= height:
            break
    for nx in range(x-1, -1, -1):
        # print('left', nx, y)
        scores[1] += 1
        if grid[y][nx] >= height:
            break
    for ny in range(y+1, len(grid)):
        # print('down', x, ny)
        scores[2] += 1
        if grid[ny][x] >= height:
            break
    for ny in range(y-1, -1, -1):
        # print('up', x, ny)
        scores[3] += 1
        if grid[ny][x] >= height:
            break
    # print(scores)
    return reduce(lambda x, y: x * y, scores, 1)

maxscore = 0
# print(calc_scenic(grid, 2, 3))
print(max(calc_scenic(grid, x, y) for x in range(len(grid[0])) for y in range(len(grid))))