summaryrefslogtreecommitdiff
path: root/08/2.lua
diff options
context:
space:
mode:
Diffstat (limited to '08/2.lua')
-rw-r--r--08/2.lua68
1 files changed, 68 insertions, 0 deletions
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)