From 3555be54c2abb8d5ece008a60dbdfbde0ffbddd7 Mon Sep 17 00:00:00 2001 From: Alex Pickering Date: Fri, 7 Feb 2025 12:49:48 -0600 Subject: inital commit --- 08/2.lua | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 08/2.lua (limited to '08/2.lua') diff --git a/08/2.lua b/08/2.lua new file mode 100644 index 0000000..691123c --- /dev/null +++ b/08/2.lua @@ -0,0 +1,68 @@ +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) -- cgit v1.2.3-70-g09d2