aboutsummaryrefslogtreecommitdiff
path: root/challenge-075/abigail/lua/ch-2.lua
blob: 0765ff81b1a84ae817e10e7879e8500aa7374a1b (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
#!/opt/local/bin/lua

--
-- See ../README.md
--

--
-- Run as: lua ch-2.lua < input-file
--

for line in io . lines () do
    local heights = {}
    local h, i
    local max_height = 0
    for h in line : gmatch ("%d+") do
        h = tonumber (h)
        table . insert (heights, h)
        if  max_height < h then
            max_height = h
        end
    end

    local max_area = 0
    for h = 1, max_height do
        local max = 0
        local cur = 0
        for i = 1, #heights do
            if heights [i] >= h then
                cur = cur + 1
            else
                if  max < cur then
                    max = cur
                end
                cur = 0
            end
        end

        if  max < cur then
            max = cur
        end

        local area = max * h
        if  max_area < area then
            max_area = area
        end
    end

    print (max_area)
end