-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.js
67 lines (57 loc) · 1.73 KB
/
day4.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
const main = () => {
const min = 123257;
const max = 647015;
let possibilities = 0;
for (let i = min; i < max + 1; i++) {
console.log('on num ', i);
if (hasDoubleDigits(i) && hasDecreasingDigits(i)) {
possibilities = possibilities + 1;
}
}
console.log('num possibilities', possibilities);
};
// Needs one of exactly two numbers
const hasDoubleDigits = (num) => {
const numString = String(num);
let lastNum = null;
for (let i = 0; i < numString.length; i++) {
const thisNum = numString[i];
if (lastNum !== null && lastNum === thisNum) {
// make sure its not the third in a sequence
if (i > 1 && i < numString.length - 1) {
if (numString[i - 2] !== thisNum && numString[i + 1] !== thisNum) {
return true;
}
} else if (i > 1) {
if (numString[i - 2] !== thisNum) {
return true;
}
} else if (i < numString.length - 1) {
if (numString[i + 1] !== thisNum) {
return true;
}
} else {
throw Error('this shouldnt happen ' + i);
}
}
lastNum = numString[i];
}
return false;
};
const hasDecreasingDigits = (num) => {
const numString = String(num);
let lastNum = null;
for (let i = 0; i < numString.length; i++) {
if (lastNum !== null) {
const thisNum = Number(numString[i]);
if (thisNum < lastNum) {
return false;
}
lastNum = thisNum;
} else {
lastNum = Number(numString[i]);
}
}
return true;
};
main();