58 lines
1.7 KiB
Python
58 lines
1.7 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)
|
||
|
# make a temporary copy of the field to track what we've already seen.
|
||
|
seen_field = [[ ' ' for y in range(len(field[0])) ] for x in range(len(field))]
|
||
|
|
||
|
trees_seen = 0
|
||
|
# [y][x]
|
||
|
# go top to bottom.
|
||
|
for x in range(len(field[0])):
|
||
|
height = -1
|
||
|
for y in range(len(field)):
|
||
|
if field[y][x] > height:
|
||
|
height = field[y][x]
|
||
|
if seen_field[y][x] != 'T':
|
||
|
seen_field[y][x] = 'T'
|
||
|
trees_seen += 1
|
||
|
# go bottom to top.
|
||
|
for x in range(len(field[0])):
|
||
|
height = -1
|
||
|
for y in reversed(range(len(field))):
|
||
|
if field[y][x] > height:
|
||
|
height = field[y][x]
|
||
|
if seen_field[y][x] != 'T':
|
||
|
seen_field[y][x] = 'T'
|
||
|
trees_seen += 1
|
||
|
|
||
|
# go left to right.
|
||
|
for y in range(len(field)):
|
||
|
height = -1
|
||
|
for x in range(len(field[0])):
|
||
|
if field[y][x] > height:
|
||
|
height = field[y][x]
|
||
|
if seen_field[y][x] != 'T':
|
||
|
seen_field[y][x] = 'T'
|
||
|
trees_seen += 1
|
||
|
# go right to left.
|
||
|
for y in range(len(field)):
|
||
|
height = -1
|
||
|
for x in reversed(range(len(field[0]))):
|
||
|
if field[y][x] > height:
|
||
|
height = field[y][x]
|
||
|
if seen_field[y][x] != 'T':
|
||
|
seen_field[y][x] = 'T'
|
||
|
trees_seen += 1
|
||
|
|
||
|
print(*seen_field, sep='\n')
|
||
|
print('trees seen: {}'.format(trees_seen))
|
||
|
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|