41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
map = []
|
|
def directionVisible(x, y, tree):
|
|
treevis = True
|
|
if type(x) == range: # Horizontal
|
|
for i in x:
|
|
if map[y][i] >= tree:
|
|
treevis = False
|
|
break
|
|
elif type(y) == range: # Vertical
|
|
for i in y:
|
|
if map[i][x] >= tree:
|
|
treevis = False
|
|
break
|
|
return treevis
|
|
|
|
with open("input.txt", "r") as f:
|
|
for line in f.readlines():
|
|
map.append(list(line.strip()))
|
|
H = len(map)
|
|
W = len(map[0])
|
|
|
|
print(f"Looking at a forest of {H} by {W}, which has {W*H} trees.")
|
|
print(f"It has {(W-2)*2+2*H} edge trees visible")
|
|
|
|
visible = 0
|
|
|
|
for y in range(H):
|
|
for x in range(W):
|
|
if x == 0 or y == 0 or x == W-1 or y == H-1:
|
|
# Edge tree
|
|
visible += 1
|
|
else:
|
|
# Center tree
|
|
tree = map[y][x]
|
|
if (
|
|
directionVisible(x, range(y-1, -1, -1), tree) or # Up
|
|
directionVisible(x, range(y+1, H), tree) or # Down
|
|
directionVisible(range(x-1, -1, -1), y, tree) or # Left
|
|
directionVisible(range(x+1, W), y, tree)): # Right
|
|
visible += 1
|
|
print(visible) |