summaryrefslogtreecommitdiff
path: root/08/2.lua
blob: 691123cf653a8e53af66633055550730f55c8c00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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)