-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_4.js
67 lines (62 loc) · 2.03 KB
/
day_4.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
// var start = 265275;
let start = 266666;
var end = 781584;
getAllPasswords(start, end);
function getAllPasswords(start, end) {
let count = 0;
for (var x = start; x <= end; x = increase(x)) {
if (_adjacentDigits(x)) {
console.log('X', x);
count++;
}
}
console.log('Found', count, 'valid numbers');
}
function _adjacentDigits(x) {
let number = x.toString().split('').map(val => val = parseInt(val));
return (number[0] === number[1] && number[1] !== number[2]) ||
(number[1] === number[2] && number[2] !== number[3] && number[2] !== number[0]) ||
(number[2] === number[3] && number[3] !== number[4] && number[3] !== number[1]) ||
(number[3] === number[4] && number[4] !== number[5] && number[4] !== number[2]) ||
(number[4] === number[5] && number[5] !== number[3]);
// return number[0] === number[1] || number[1] === number[2] || number[2] === number[3] || number[3] === number[4] || number[4] === number[5];
}
function increase(x) {
if (x % 10 === 9) {
//The number is about to be increased, but a zero is going to be less than the tens digit.
let number = x.toString().split('').map(val => val = parseInt(val));
if (number[4] < 9) {
number[4] = number[4] + 1;
number[5] = number[4];
}
else if (number[3] < 9) {
number[3] = number[3] + 1;
number[4] = number[3];
number[5] = number[3];
}
else if (number[2] < 9) {
number[2] = number[2] + 1;
number[3] = number[2];
number[4] = number[2];
number[5] = number[2];
}
else if (number[1] < 9) {
number[1] = number[1] + 1;
number[2] = number[1];
number[3] = number[1];
number[4] = number[1];
number[5] = number[1];
}
else if (number[0] < 9) {
number[0] = number[0] + 1;
number[1] = number[0];
number[2] = number[0];
number[3] = number[0];
number[4] = number[0];
number[5] = number[0];
}
//else the number just became 7 digits
return parseInt(number.join(''));
}
else return x + 1;
}