-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (65 loc) · 2.09 KB
/
index.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// how many numbers should we test?
const numbers = 12_000_000;
// which number should we start at
const startAt = 0;
// prevent lots of duplicate digits - i.e. 224444, 442244, 444422, 666666
const strictMode = true;
function isEven(n) {
return n % 2 == 0;
}
function splitNumber(n) {
n = n.toString();
var result = [];
for (var i = 0; i <= n.length-2; i += 2) {
result.push(n.slice(i, i+2));
}
return result;
}
function countOccurences(numberToLookFor, numberToLookIn) {
numberToLookIn = numberToLookIn.toString();
var count = 0;
numberToLookIn.split('').forEach(n => {
n = parseInt(n);
if (n == numberToLookFor) { count++; }
});
return count;
}
function findDuplicates(arry) {
return arry.filter((item, index) => arry.indexOf(item) !== index);
}
function main() {
console.log('Starting number crunch!');
const startTime = Date.now();
var results = [];
numberCrunch:
for (var i = 0; i < numbers; i++) {
// example number: 14233221
var number = i;
// check number has even number of digits
if (!isEven(number.toString().length)) { continue }
// split our number into couplets
// example: ['14', '23', '32', '21']
var couplets = splitNumber(number);
// strict mode: there should not be duplicate couplets
if (strictMode && findDuplicates(couplets).length) {
continue numberCrunch;
}
for (var x = 0; x < couplets.length; x++) {
// each couplet indicates how often an integer should appear in our number
// example: ['14'] - there should be x1 4 in our number
// lets check each couplet to see if they're telling the truth
if (parseInt(couplets[x].charAt(0)) !== countOccurences(couplets[x].charAt(1), number)) {
continue numberCrunch;
}
// make sure each quantity indicator gets mentioned/tested
if (!couplets.map(e => e.charAt(1)).includes(couplets[x].charAt(0))) {
continue numberCrunch;
}
}
// our number passed the tests!
results.push(number);
}
console.log(`Done - took ${Date.now() - startTime}ms`);
console.log(results);
}
main();