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

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

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

  require ('readline')
. createInterface ({input: process . stdin})   
. on              ('line', (num) => {
    let number = "0";
    for (let i = 0; i < + num; i ++) {
        number = next_number (number)
    }
    console . log (number)
})


function next_number (prev_number) {
    //
    // Grab the trailing 3s (tail), its preceding number (num), and
    // anything before that (prefix).
    //
    let [match, prefix, num, tail] =
        ("0" + prev_number) . match (/^(.*)([012])(3*)$/)

    //
    // First, we take the prefix, and add (num + 1) to it,
    // then the tail, where we have replaced each 3 by a 1.
    // Then replace any '11' with '12' (we can only have 11s at the end).
    // Finally, remove any leading 0.
    //
    return (prefix + (+ num + 1) + (tail . replace (/3/g,  "1"))) .
                                           replace (/11/g, "12")  .
                                           replace (/^0/,  "")
}