-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.js
45 lines (37 loc) · 941 Bytes
/
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
const low = 134792;
const high = 675810;
const hasAdjacentDouble = x => {
const arr = x.toString().split("");
for (let i = 1; i < 6; i++) {
if (arr[i] === arr[i - 1]) return true;
}
return false;
};
const hasIncreasingDigits = x => {
const arr = x
.toString()
.split("")
.map(v => parseInt(v, 10));
for (let i = 1; i < 6; i++) {
if (arr[i] < arr[i - 1]) return false;
}
return true;
};
const hasAdjacentDoubleOnly = x => {
const arr = x
.toString()
.split("")
.reduce((acc, d) => {
if (acc.has(d)) acc.set(d, acc.get(d) + 1);
else acc.set(d, 1);
return acc;
}, new Map());
return [...arr].filter(v => v[1] === 2).length > 0;
};
let count1 = 0;
let count2 = 0;
for (let x = low; x <= high; x++) {
if (hasAdjacentDouble(x) && hasIncreasingDigits(x)) count1++;
if (hasAdjacentDoubleOnly(x) && hasIncreasingDigits(x)) count2++;
}
console.log(count1, count2);