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

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

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

function make_sequence (string, start)
    if   string == start
    then return start
    end
    if   string : find ("^" .. start)
    then tail   = string : sub (#start + 1, -1)
         result = make_sequence (tail, tostring (tonumber (start) + 1))
         if   result ~= nil
         then return start .. "," .. result
         end
         result = make_sequence (tail, tostring (tonumber (start) - 1))
         if   result ~= nil
         then return start .. "," .. result
         end
    end
    return nil
end

for line in io . lines () do
    for i = 1, #line do
        start  = line : sub (1, i)
        result = make_sequence (line, start)
        if   result ~= nil
        then print (result)
             goto end_main
        end
    end
    ::end_main::
end