aboutsummaryrefslogtreecommitdiff
path: root/challenge-141/abigail/node/ch-2.js
blob: 53b7b7ac4438fff8e7bbc528c7ac0089501d0abf (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
#!/usr/local/bin/node

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

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

function substrings (n, m, prefix, max) {
    if (n . length == 0) {
        return prefix > -1   &&
               prefix < max  &&
               prefix % m == 0 ? 1 : 0
    }

    let fc       = n . substr (0, 1)
    let tail     = n . substr (1)
    let n_prefix = 10 * (prefix == -1 ? 0 : prefix) + + n . substr (0, 1)

    return substrings (tail, m, n_prefix, max) +
           substrings (tail, m, prefix,   max);
}

  require ('readline')
. createInterface ({input: process . stdin})   
. on              ('line', line => {
    let [n, m] = line . split (" ")
    console . log (substrings (n, +m, -1, +n))
})