-
Notifications
You must be signed in to change notification settings - Fork 0
/
dayFour.js
41 lines (35 loc) · 1.04 KB
/
dayFour.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
const fs = require("fs");
const data = fs.readFileSync("./dayFour.txt").toString();
const lines = data.split("\n");
// console.log(lines);
var newArray = [];
for (var i = 0; i < lines.length; i++) {
var assignments = lines[i].split(",");
newArray.push(assignments);
}
// console.log(newArray);
const checkPairs = newArray.filter((pair) => {
const numberArray = pair.map((num) =>
num.split("-").map((string) => Number(string))
);
// console.log(numberArray);
return (
(numberArray[0][0] <= numberArray[1][0] &&
numberArray[0][1] >= numberArray[1][1]) ||
(numberArray[0][0] >= numberArray[1][0] &&
numberArray[0][1] <= numberArray[1][1])
);
});
console.log(checkPairs.length);
//part Two
const checkOverLappingPairs = newArray.filter((pair) => {
const numberArray = pair.map((num) =>
num.split("-").map((string) => Number(string))
);
console.log(numberArray);
return (
numberArray[0][0] <= numberArray[1][1] &&
numberArray[0][1] >= numberArray[1][0]
);
});
console.log(checkOverLappingPairs.length);