Skip to content

Instantly share code, notes, and snippets.

@LordSmurf
Created December 7, 2022 11:57
Show Gist options
  • Save LordSmurf/6d7101efe27c4e0839de80a7ce80c2ea to your computer and use it in GitHub Desktop.
Save LordSmurf/6d7101efe27c4e0839de80a7ce80c2ea to your computer and use it in GitHub Desktop.
AOC 2022 - Day 4
const fs = require("fs/promises");
const _ = require("lodash");
const mainInput = "input.txt";
const example = "example.txt";
const example2 = "example2.txt";
async function readInput(fileName) {
const file = await fs.readFile(fileName, "utf-8");
return file.trim().replace(/\r/g, "").split("\n");
}
async function solveFirst(fileName) {
const inputs = await readInput(mainInput);
let totalScrewUps = 0;
inputs.forEach((pair) => {
let sectors = pair.split(",");
let subSectors1 = sectors[0].split("");
let subSectors2 = sectors[1].split("");
let sec1Range = [];
let sec2Range = [];
for (let i = subSectors1[0]; i <= subSectors1[2]; i++) {
sec1Range.push(Number(i));
}
for (let i = subSectors2[0]; i <= subSectors2[2]; i++) {
sec2Range.push(Number(i));
}
let assign1Comp = sec1Range.every((element) => sec2Range.includes(element));
let assign2Comp = sec2Range.every((element) => sec1Range.includes(element));
if (assign2Comp) {
totalScrewUps += 1;
}
});
return totalScrewUps;
}
async function solveTwo(fileName) {}
(async () => {
const partOne = await solveFirst(mainInput);
// const partTwo = await solveTwo(mainInput);
console.log(partOne);
// console.log(partTwo);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment