-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-04.js
51 lines (38 loc) · 1.53 KB
/
day-04.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
const fs = require("fs");
const input = fs.readFileSync("input/day-04.txt", "utf8");
// Split input into lines
const lines = input.split("\n");
// Part one: find how many ranges are contained in the other
const containedRangesCounter = lines.reduce((acc, line) => {
// Split line into two the two ranges
const ranges = line.split(",");
const firstElf = ranges[0].split("-").map((n) => parseInt(n));
const secondElf = ranges[1].split("-").map((n) => parseInt(n));
// Check if the first range is contained in the second range
if (secondElf[0] >= firstElf[0] && secondElf[1] <= firstElf[1]) {
return acc + 1;
}
// Check if the second range is part of the first one
if (firstElf[0] >= secondElf[0] && firstElf[1] <= secondElf[1]) {
return acc + 1;
}
return acc;
}, 0);
console.log("containedRangesCounter", containedRangesCounter);
// Part two: find how many ranges overlap
const overlappingRangesCounter = lines.reduce((acc, line) => {
// Split line into two the two ranges
const ranges = line.split(",");
const firstElf = ranges[0].split("-").map((n) => parseInt(n));
const secondElf = ranges[1].split("-").map((n) => parseInt(n));
// Check if the first range overlaps the second range
if (secondElf[0] <= firstElf[1] && secondElf[1] >= firstElf[1]) {
return acc + 1;
}
// Check if the second range overlaps the first range
if (firstElf[0] <= secondElf[1] && firstElf[1] >= secondElf[1]) {
return acc + 1;
}
return acc;
}, 0);
console.log("overlappingRangesCounter", overlappingRangesCounter);