adventofcode2022/04/code.js

43 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-12-04 05:24:18 +00:00
const fs = require('fs');
const inputArray = fs.readFileSync('input.txt').toString().split("\n");
// Part One
let totalCount = 0;
for (i in inputArray) {
const contents = inputArray[i].split(",");
const range1 = contents[0].split("-");
const range2 = contents[1].split("-");
if (
(parseInt(range1[0]) >= parseInt(range2[0]) && parseInt(range1[1]) <= parseInt(range2[1]) && parseInt(range1[1]) <= parseInt(range2[1]))
||
(parseInt(range2[0]) >= parseInt(range1[0]) && parseInt(range2[1]) <= parseInt(range1[1]) && parseInt(range2[1]) <= parseInt(range1[1]))
) {
totalCount++
}
}
console.log(totalCount);
// Part Two
totalCount = 0;
for (i in inputArray) {
const contents = inputArray[i].split(",");
const range1 = contents[0].split("-");
const range2 = contents[1].split("-");
if (
(parseInt(range1[1]) >= parseInt(range2[0]) && parseInt(range1[1]) <= parseInt(range2[1]) && parseInt(range1[1]) <= parseInt(range2[1]))
||
(parseInt(range2[1]) >= parseInt(range1[0]) && parseInt(range2[1]) <= parseInt(range1[1]) && parseInt(range2[1]) <= parseInt(range1[1]))
) {
totalCount++
}
}
console.log(totalCount);