advent_of_code/2022/8/8.py

67 lines
1.6 KiB
Python
Raw Permalink Normal View History

2022-12-10 14:14:21 +01:00
#!/usr/bin/env python
# 2022 - Advent Of Code 8
# 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, t in enumerate(tree_map):
# view from left
visible_height = -1
for x, tree in enumerate(t):
if tree > visible_height:
visible_map[y][x] = 1
visible_height = tree
# view from right
visible_height = -1
for x, tree in reversed(list(enumerate(t))):
if tree > visible_height:
visible_map[y][x] = 1
visible_height = tree
for x in range(len(tree_map[0])):
visible_height = -1
# from top
for y in range(len(tree_map[0])):
if tree_map[y][x] > visible_height:
visible_map[y][x] = 1
visible_height = tree_map[y][x]
# from bottom
visible_height = -1
for y in reversed(range(len(tree_map[0]))):
if tree_map[y][x] > visible_height:
visible_map[y][x] = 1
visible_height = tree_map[y][x]
for t in tree_map:
print(t)
print()
for v in visible_map:
print(v)
count = 0
for t in visible_map:
for x, tree in enumerate(t):
if t[x] == 1:
count += 1
print(f'count: {count}')