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

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

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

function possibilities (target, coins, from, to)
    if target == 0 then
        return 1
    end
    if target < 0 or from > to then
        return 0
    end

    local sum = 0

    for i = 0, math . floor (target / coins [from]) do
        sum = sum + possibilities (target - i * coins [from],
                                                coins, from + 1, to)
    end

    return sum
end


for line in io . lines () do
    local target
    local coins = {}
    local i
    for i in line : gmatch ("%d+") do
        if  target == nil then
            target = tonumber (i)
        else
            table . insert (coins, tonumber (i))
        end
    end
    print (possibilities (target, coins, 1, #coins))
 
end