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

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

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

function next_number (prev_num)
    --
    -- Get the trailing 3s (tail), and the number before (num)
    -- and anything before it (prefix)
    --
    local _, _, prefix, num, tail =
        ("0" .. prev_num) : find ("(.*)([012])(3*)$")

    --
    -- Combine the prefix, num (with 1 added) and the tail (with
    -- its 3s replaced by 1s), then in the result, replace each '11'
    -- with '12', and remove the leading 0 (if any).
    --
    return (prefix .. tostring (tonumber (num) + 1) 
                   .. tail : gsub ("3", "1")) : gsub ("11", "12")
                                              : gsub ("^0", "")
end


for num in io . lines () do
    local number = "0"
    for _ = 1, tonumber (num) do
        number = next_number (number)
    end
    print (number)
end