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

--
-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-003
--

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


--
-- Iterate over the input
--
for line in io . lines () do
    local rows = tonumber (line)

    --
    -- Create and print row 0
    --
    local row = {1}
    print "1"

    local r
    for r = 1, rows do
        -- 
        -- Create a new row
        --
        local new = {}
        for i = 1, r + 1 do
            sum = 0;
            if i > 1
            then sum = sum + row [i - 1]
                 io . write (" ")
            end
            if i <= r
            then sum = sum + row [i]
            end
            --
            -- Assign new element, and print it
            --
            new [i] = sum
            io . write (sum)
        end
        io . write ("\n")
        --
        -- New row becomes current row
        --
        row = new
    end
end