aboutsummaryrefslogtreecommitdiff
path: root/challenge-075/abigail/node/ch-1.js
blob: 99ea6b661e7e483fe67d7dc51f849752a86cdc79 (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
#!/usr/local/bin/node

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

//
// Run as: node ch-1.js < input-file
//

function possibilities (target, coins, from, to) {
    if (target == 0) {
        return (1)
    }
    if (target < 0 || from > to) {
        return (0)
    }

    let sum = 0
    let i
    for (i = 0; i * coins [from] <= target; i ++) {
        sum += possibilities (target - i * coins [from], coins, from + 1, to)
    }

    return sum
}

  require ('readline')
. createInterface ({input: process . stdin})   
. on              ('line', line => {
    let coins = line . split (' ') . map (_ => +_)
    console . log (possibilities (coins [0], coins, 1, coins . length - 1))
})