first commit

This commit is contained in:
2025-12-03 00:04:09 -05:00
commit b87fb99001
10 changed files with 4867 additions and 0 deletions

45
day02/js/day02a.js Normal file
View File

@@ -0,0 +1,45 @@
import { promises as fsp } from "node:fs";
async function asyncReadFile(filename) {
try {
const contents = await fsp.readFile(filename, "utf-8");
return contents.trim();
} catch (err) {
console.error(err);
}
}
/*
const dataToParse =
"11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124";
*/
const dataToParse = await asyncReadFile("../input.txt");
const isDuplicate = (val = "") => {
const strVal = `${val}`,
strLen = strVal.length;
if (!strLen || strLen % 2 === 1) return false;
return strVal.substring(0, strLen / 2) === strVal.substring(strLen / 2);
};
const ranges = dataToParse.split(",");
let total = 0;
ranges.forEach((range) => {
console.log(`> running range ${range}`);
const [start, end] = range.split("-"),
last = end * 1;
let curr = start * 1;
while (curr <= last) {
if (isDuplicate(curr)) {
total += curr;
console.log(`>> found duplicate: ${curr} / ${total}`);
}
curr++;
}
});
console.log(`> FINAL: ${total}`);