generated from eric/adventofcode2023
day five both parts
This commit is contained in:
97
05/code.js
Normal file
97
05/code.js
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
import { readFileSync } from 'node:fs';
|
||||||
|
|
||||||
|
let sampleMode = false;
|
||||||
|
let usedArray = [];
|
||||||
|
|
||||||
|
const sampleArray = readFileSync('sample.txt').toString().split("\n");
|
||||||
|
const inputArray = readFileSync('input.txt').toString().split("\n");
|
||||||
|
|
||||||
|
if (sampleMode) {
|
||||||
|
usedArray = sampleArray;
|
||||||
|
} else {
|
||||||
|
usedArray = inputArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Part One
|
||||||
|
|
||||||
|
console.time("part1");
|
||||||
|
|
||||||
|
const ranges = [];
|
||||||
|
const ingredients = [];
|
||||||
|
const validIngredients = [];
|
||||||
|
|
||||||
|
for (const element of usedArray) {
|
||||||
|
if (element.includes("-")) {
|
||||||
|
const contents = element.split("-");
|
||||||
|
const start = Number.parseInt(contents[0]);
|
||||||
|
const end = Number.parseInt(contents[1]);
|
||||||
|
ranges.push({ start, end });
|
||||||
|
} else if (element !== "") {
|
||||||
|
ingredients.push(Number.parseInt(element));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const ingredient of ingredients) {
|
||||||
|
for (const range of ranges) {
|
||||||
|
if (ingredient >= range.start && ingredient <= range.end) {
|
||||||
|
validIngredients.push(ingredient);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.timeEnd("part1");
|
||||||
|
console.log(validIngredients.length);
|
||||||
|
|
||||||
|
|
||||||
|
// Part Two
|
||||||
|
|
||||||
|
console.time("part2");
|
||||||
|
|
||||||
|
const ranges2 = [];
|
||||||
|
|
||||||
|
for (const element of usedArray) {
|
||||||
|
if (element.includes("-")) {
|
||||||
|
const contents = element.split("-");
|
||||||
|
const start = Number.parseInt(contents[0]);
|
||||||
|
const end = Number.parseInt(contents[1]);
|
||||||
|
ranges2.push({ start, end });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ranges2.sort((a, b) => a.start - b.start);
|
||||||
|
let processing = true;
|
||||||
|
|
||||||
|
while (processing) {
|
||||||
|
compactRanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Five more times for fun, just in case
|
||||||
|
for (let i = 0; i < 5; i++) {
|
||||||
|
compactRanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
let sum = 0;
|
||||||
|
for (const range of ranges2) {
|
||||||
|
sum += range.end - range.start + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.timeEnd("part2");
|
||||||
|
console.log(sum);
|
||||||
|
|
||||||
|
// functions
|
||||||
|
|
||||||
|
function compactRanges() {
|
||||||
|
let continuing = false;
|
||||||
|
let i = 0;
|
||||||
|
while (i < ranges2.length - 1) {
|
||||||
|
if (ranges2[i + 1].start <= ranges2[i].end + 1) {
|
||||||
|
ranges2[i].end = Math.max(ranges2[i].end, ranges2[i + 1].end);
|
||||||
|
ranges2.splice(i + 1, 1);
|
||||||
|
continuing = true;
|
||||||
|
} else {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
processing = continuing;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user