You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.9 KiB
Python

#!/usr/bin/env python3
INPUT_FILE = 'input'
def main():
field = [[ int(n) for n in line ] for line in map(str.strip, open(INPUT_FILE, 'r').readlines()) ]
print(*field, sep='\n')
print('#' * 20)
field_height = len(field)
field_width = len(field[0])
# make a temporary copy of the field to track what we've already seen.
score_field = [[ 0 for y in range(len(field[0])) ] for x in range(len(field))]
# [y][x]
# iterate every tree, calculate scenic score
for x in range(len(field[0])):
for y in range(len(field)):
house_height = field[y][x]
view_distances = []
# check upwards.
current_height = -1
seen_trees = 0
for y_off in reversed(range(0, y)):
if field[y_off][x] < house_height:
#if field[y_off][x] > current_height:
# current_height = field[y_off][x]
seen_trees += 1
else:
seen_trees += 1
break
view_distances.append(seen_trees)
# check left.
current_height = -1
seen_trees = 0
for x_off in reversed(range(0, x)):
if field[y][x_off] < house_height:
#if field[y][x_off] > current_height:
# current_height = field[y][x_off]
seen_trees += 1
else:
seen_trees += 1
break
view_distances.append(seen_trees)
# check downwards.
current_height = -1
seen_trees = 0
for y_off in range(y+1, field_height):
if field[y_off][x] < house_height:
#if field[y_off][x] > current_height:
# current_height = field[y_off][x]
seen_trees += 1
else:
seen_trees += 1
break
view_distances.append(seen_trees)
# check right.
current_height = -1
seen_trees = 0
for x_off in range(x+1, field_width):
if field[y][x_off] < house_height:
#if field[y][x_off] > current_height:
# current_height = field[y][x_off]
seen_trees += 1
else:
seen_trees += 1
break
view_distances.append(seen_trees)
if x == 2 and y == 3:
print(view_distances)
# get sum.
score = 1
for num in view_distances:
score *= num
score_field[y][x] = score
print(*score_field, sep='\n')
best = max([max(_) for _ in score_field])
print('Best score: {}'.format(best))
if __name__ == '__main__':
main()