def is_visible(i, j): this = grid[i][j] surround = [ [grid[i2][j] for i2 in range(i)], [grid[i][j2] for j2 in range(j)], [grid[i2][j] for i2 in range(i+1, len(grid))], [grid[i][j2] for j2 in range(j+1, len(grid[0]))], ] return any([all([s < this for s in sur]) for sur in surround]) def view(i, j): score = 1 this = grid[i][j] surround = [ [grid[i2][j] for i2 in range(i-1, -1, -1)], [grid[i][j2] for j2 in range(j-1, -1, -1)], [grid[i2][j] for i2 in range(i+1, len(grid))], [grid[i][j2] for j2 in range(j+1, len(grid[0]))], ] for sur in surround: surlen = len(sur) i = 0 while i < surlen and sur[i] < this: i += 1 if i < surlen and sur[i] >= this: i += 1 score *= i return score #with open("example") as f: with open("input") as f: grid = [line.strip() for line in f.readlines()] width, height = len(grid[0]), len(grid) edge_visible = (width + height-2) * 2 highest = 0 for i in range(1, height-1): for j in range(1, width-1): score = view(i, j) if score > highest: highest = score print(highest)