#!/usr/bin/env python TGRID = [] # heights VGRID = [] # scenic score def count_visible(x, y, xr, yr): target = TGRID[y][x] count = 0 for y in yr: for x in xr: if TGRID[y][x] >= target: if x > 0 and y > 0: count += 1 return count count+=1 return count with open('input') as f: for line in f: l = [] for tree in line.rstrip(): l.append(int(tree)) TGRID.append(l) max_y = len(TGRID) for y in range(0, max_y): max_x = len(TGRID[y]) l = [] for x in range(0, max_x): l.append( count_visible(x, y, range(x, x+1), range(y-1, -1, -1)) * # north count_visible(x, y, range(x+1, max_x), range(y, y+1)) * # east count_visible(x, y, range(x, x+1), range(y+1, max_y)) * # south count_visible(x, y, range(x-1, -1, -1), range(y, y+1)) # west ) VGRID.append(l) best_seen = 0 for y in range(0, len(VGRID)): for x in range(0, len(VGRID[y])): if VGRID[y][x] > best_seen: best_seen = VGRID[y][x] print(best_seen)