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 isDuplicateOld = (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 duplicateRE = /^(\d+)\1+$/; const isDuplicate = (val = "") => { // new process - runa regex that looks for repeats from start to end const strVal = `${val}`, strLen = strVal.length; if (!strLen) return false; const matches = strVal.match(duplicateRE); if (matches === null) return false; return matches.length >= 1 && matches[0] === strVal; }; 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}`);