-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.js
53 lines (41 loc) · 1.01 KB
/
day04.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
const {
datefy,
} = require('./utils');
const data = datefy(4, x => x.split(',').map(c => c.split('-').toIntArray()));
const example = [
[[2, 4], [6, 8]],
[[2, 3], [4, 5]],
[[5, 7], [7, 9]],
[[2, 8], [3, 7]],
[[6, 6], [4, 6]],
[[2, 6], [4, 8]],
];
function inRange(n, b, e) {
return n >= b && n <= e;
}
function fullyContained(pairs) {
let count = 0;
for (const pair of pairs) {
const [a, b] = pair[0];
const [c, d] = pair[1];
if ((inRange(a, c, d) && inRange(b, c, d)) || (inRange(c, a, b) && inRange(d, a, b))) {
count+= 1;
}
}
return count;
}
console.log(fullyContained(example)); // -> 2
console.log(fullyContained(data)); // -> 487
function contained(pairs) {
let count = 0;
for (const pair of pairs) {
const [a, b] = pair[0];
const [c, d] = pair[1];
if (inRange(a, c, d) || inRange(b, c, d) || inRange(c, a, b) || inRange(d, a, b)) {
count+= 1;
}
}
return count;
}
console.log(contained(example)); // -> 4
console.log(contained(data)); // -> 849