update with day04 and first puzzle of day05

This commit is contained in:
2025-12-05 01:36:41 -05:00
parent 1b2b0f4edd
commit 93f795cd2c
8 changed files with 1640 additions and 1 deletions

80
day05/js/day05a.js Normal file
View File

@@ -0,0 +1,80 @@
import { promises as fsPromises } from "node:fs";
async function asyncReadFile(filename) {
try {
const contents = await fsPromises.readFile(filename, "utf-8");
const arr = contents.split(/\r?\n/);
return arr;
} catch (err) {
console.error(err);
}
}
const parseData = (rows) => {
let isInRanges = true;
let freshRanges = [];
let freshCount = 0;
rows.forEach((row) => {
if (isInRanges) {
if (row === "") {
isInRanges = false;
return;
}
if (row.includes("-")) {
freshRanges.push(row);
/*
const [start, end] = row.split("-").map((val) => val * 1);
for (let i = start; i <= end; i++) {
if (!freshIngredients.includes(i)) freshIngredients.push(i);
}
*/
return;
}
if (/^\d+$/.matches(row) && !freshIngredients.includes(row * 1)) {
// freshIngredients.push(row * 1);
freshRanges.push(row);
return;
}
return;
}
let iNum = row * 1;
let isFresh = freshRanges.some((range) => {
if (!range.includes("-")) return range === `${iNum}`;
const [start, end] = range.split(/-/).map((val) => val * 1);
// console.log({ range, iNum, start, end });
return iNum >= start && iNum <= end;
});
if (isFresh) freshCount++;
console.log(`Ingredient ${row} is ${isFresh ? "fresh" : "spoiled"}.`);
});
return freshCount;
};
const testData = `
3-5
10-14
16-20
12-18
1
5
8
11
17
32
`.trim();
// const rows = testData.split("\n");
const rows = await asyncReadFile("../input.txt");
console.log(`Fresh ingredient count: ${parseData(rows)}`);