89 lines
2.0 KiB
Python
89 lines
2.0 KiB
Python
import os
|
|
|
|
input = open(os.path.dirname(__file__) +
|
|
"/input.txt", "r").readlines()
|
|
|
|
trees = []
|
|
|
|
for l in input:
|
|
l = l.strip()
|
|
tmp = []
|
|
for x in l:
|
|
tmp.append([int(x), False])
|
|
trees.append(tmp)
|
|
|
|
rows = len(trees)
|
|
cols = len(trees[0])
|
|
|
|
|
|
def calcScore(x: int, y: int):
|
|
h = trees[y][x][0]
|
|
ls, rs, us, ds = 1, 1, 1, 1
|
|
|
|
# right
|
|
i = x+1
|
|
while (i < cols-1 and trees[y][i][0] < h):
|
|
i += 1
|
|
rs += 1
|
|
|
|
# left
|
|
i = x-1
|
|
while (i > 0 and trees[y][i][0] < h):
|
|
i -= 1
|
|
ls += 1
|
|
|
|
# down
|
|
i = y+1
|
|
while (i < rows - 1 and trees[i][x][0] < h):
|
|
i += 1
|
|
ds += 1
|
|
|
|
# up
|
|
i = y-1
|
|
while (i > 0 and trees[i][x][0] < h):
|
|
i -= 1
|
|
us += 1
|
|
|
|
return ls*rs*us*ds
|
|
|
|
|
|
scenic_scores = []
|
|
|
|
for x in range(0, cols, 1):
|
|
height = 0
|
|
for y in range(0, cols, 1):
|
|
if trees[y][x][0] > height or (x == 0) or (y == 0) or (x == cols-1) or (y == rows-1):
|
|
trees[y][x][1] = True
|
|
height = trees[y][x][0]
|
|
|
|
height = 0
|
|
for y in range(cols-1, -1, -1):
|
|
if trees[y][x][0] > height or (x == 0) or (y == 0) or (x == cols-1) or (y == rows-1):
|
|
trees[y][x][1] = True
|
|
height = trees[y][x][0]
|
|
|
|
|
|
for y in range(0, rows, 1):
|
|
height = 0
|
|
for x in range(0, cols, 1):
|
|
if trees[y][x][0] > height or (x == 0) or (y == 0) or (x == cols-1) or (y == rows-1):
|
|
trees[y][x][1] = True
|
|
height = trees[y][x][0]
|
|
if (x != 0) and (y != 0) and (x != cols-1) and (y != rows-1):
|
|
scenic_scores.append(calcScore(x, y))
|
|
|
|
height = 0
|
|
for x in range(cols-1, -1, -1):
|
|
if trees[y][x][0] > height or (x == 0) or (y == 0) or (x == cols-1) or (y == rows-1):
|
|
trees[y][x][1] = True
|
|
height = trees[y][x][0]
|
|
|
|
tot = 0
|
|
for y in range(0, rows, 1):
|
|
for x in range(0, cols, 1):
|
|
if trees[y][x][1]:
|
|
tot += 1
|
|
|
|
print("Part 1: " + str(tot))
|
|
print("Part 2: " + str(max(scenic_scores)))
|