-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfour.js
More file actions
69 lines (61 loc) · 1.21 KB
/
Copy pathfour.js
File metadata and controls
69 lines (61 loc) · 1.21 KB
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
const { readFile } = require('fs/promises');
// 4
async function init() {
const input = await readFile('four.txt', 'utf8');
// const input = await readFile('four.test.txt', 'utf8');
const data = input.split('\n');
let countA = 0;
let countB = 0;
data.forEach((d) => {
const [a, b] = d.split(',');
let [a1, a2] = a.split('-');
let [b1, b2] = b.split('-');
a1 = parseInt(a1);
a2 = parseInt(a2);
b1 = parseInt(b1);
b2 = parseInt(b2);
if (enclosed(a1, a2, b1, b2)) {
countA++;
}
if (overlap(a1, a2, b1, b2)) {
countB++;
}
});
console.log({ countA, countB });
}
function enclosed(a1, a2, b1, b2) {
if (a1 <= b1 && a2 >= b2) {
return true;
}
if (b1 <= a1 && b2 >= a2) {
return true;
}
return false;
}
function overlap(a1, a2, b1, b2) {
// if (a1 == b1 || a1 == b2 || a2 == b1 || a2 == b2) {
// return true;
// }
if (a1 <= b1) {
if (a2 >= b1) {
return true;
}
}
if (b1 <= a1) {
if (b2 >= a1) {
return true;
}
}
// if (a2 >= b2) {
// if (a1 <= b2) {
// return true;
// }
// }
// if (b2 >= a2) {
// if (b1 <= a2) {
// return true;
// }
// }
return false;
}
init();