require "ext" local heights = {} for line in io.lines() do local trees = {} line:gsub("%d",function(t) local tree = { height=tonumber(t), scene = {up=0,down=0,left=0,right=0} } table.insert(trees,tree) end) table.insert(heights,trees) end print("heights:",heights) local nrow,ncol = #heights, #heights[1] local maxscene = 0 local function is_visible(row,col) print("examining tree at ",row,col,heights[row][col].height) local this_tree = heights[row][col] --up for i = row-1,1,-1 do if this_tree.height > heights[i][col].height then this_tree.scene.up = this_tree.scene.up + 1 else this_tree.scene.up = this_tree.scene.up + 1 break end end --down for i = row+1,nrow do if this_tree.height > heights[i][col].height then this_tree.scene.down = this_tree.scene.down + 1 else this_tree.scene.down = this_tree.scene.down + 1 break end end --left for i = col-1,1,-1 do if this_tree.height > heights[row][i].height then this_tree.scene.left = this_tree.scene.left + 1 else this_tree.scene.left = this_tree.scene.left + 1 break end end --right for i = col+1,ncol do if this_tree.height > heights[row][i].height then this_tree.scene.right = this_tree.scene.right + 1 else this_tree.scene.right = this_tree.scene.right + 1 break end end local s = this_tree.scene this_tree.scenescore = s.up * s.down * s.left * s.right if this_tree.scenescore > maxscene then maxscene = this_tree.scenescore end end for row = 2, nrow-1 do for col = 2, ncol-1 do is_visible(row,col) end end print(maxscene)