Solution for day four

This commit is contained in:
Eric Wagoner 2022-12-04 00:24:18 -05:00
parent e1fcb62390
commit 0799312f03
2 changed files with 1067 additions and 0 deletions

67
04/code.js Normal file
View File

@ -0,0 +1,67 @@
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);
// functions
function getBadge(arrs) {
let badge = arrs[0];
for(let i = 1; i < arrs.length; i++){
badge = intersection(badge, arrs[i]);
};
return badge[0];
}
function getIntersection(contents) {
const length = contents.length;
const first = contents.substring(0, length / 2).split("");
const second = contents.substring(length / 2).split("");
return intersection(first, second)[0];
}
function getPriority(char) {
const characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
return characters.indexOf(char) + 1;
}
function intersection(first, second) {
return first.filter(x => second.includes(x));
};

1000
04/input.txt Normal file

File diff suppressed because it is too large Load Diff