61 lines
1.6 KiB
Python
61 lines
1.6 KiB
Python
|
#!/usr/bin/env python
|
||
|
# 2022 - Advent Of Code 8 part 2
|
||
|
|
||
|
file = 'input_example.txt'
|
||
|
file = 'input.txt'
|
||
|
|
||
|
|
||
|
def parse_file(input_file):
|
||
|
tmap = []
|
||
|
vmap = []
|
||
|
with open(input_file, encoding="utf-8") as f:
|
||
|
for line in f.readlines():
|
||
|
tline = []
|
||
|
vline = []
|
||
|
for c in line.strip():
|
||
|
tline.append(int(c))
|
||
|
vline.append(0)
|
||
|
tmap.append(tline)
|
||
|
vmap.append(vline)
|
||
|
return tmap, vmap
|
||
|
|
||
|
|
||
|
tree_map, visible_map = parse_file(file)
|
||
|
for y in range(1, len(tree_map) - 1):
|
||
|
for x in range(1, len(tree_map[0]) - 1):
|
||
|
# calculate scenic score
|
||
|
up = down = left = right = 1
|
||
|
cur_height = tree_map[y][x]
|
||
|
var = y - 1
|
||
|
while var > 0 and tree_map[var][x] < cur_height:
|
||
|
up += 1
|
||
|
var -= 1
|
||
|
var = y + 1
|
||
|
while var < len(tree_map)-1 and tree_map[var][x] < cur_height:
|
||
|
down += 1
|
||
|
var += 1
|
||
|
var = x - 1
|
||
|
while var > 0 and tree_map[y][var] < cur_height:
|
||
|
left += 1
|
||
|
var -= 1
|
||
|
var = x + 1
|
||
|
while var < len(tree_map[0])-1 and tree_map[y][var] < cur_height:
|
||
|
right += 1
|
||
|
var += 1
|
||
|
|
||
|
scenic_score = cur_height
|
||
|
# print(f'up: {up}, down: {down}, left: {left}, right: {right}')
|
||
|
scenic_score = up * down * left * right
|
||
|
visible_map[y][x] = scenic_score
|
||
|
|
||
|
for v in visible_map:
|
||
|
print(v)
|
||
|
|
||
|
score_max = 0
|
||
|
for y in range(1, len(tree_map) - 1):
|
||
|
for x in range(1, len(tree_map[0]) - 1):
|
||
|
if visible_map[y][x] > score_max:
|
||
|
score_max = visible_map[y][x]
|
||
|
|
||
|
print(f'max scenic score: {score_max}')
|