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}`);