From 06de865c1294c3c722b2c110061f3ad04b04c3b6 Mon Sep 17 00:00:00 2001 From: Eric Wagoner Date: Tue, 2 Dec 2025 12:56:37 -0500 Subject: [PATCH] day two part two --- 02/code.js | 35 +++++++++++++++++++++++++++++++---- 03/code.js | 7 ++++--- day-template/code.js | 5 ++++- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/02/code.js b/02/code.js index 68ac12b..d103ca3 100644 --- a/02/code.js +++ b/02/code.js @@ -1,10 +1,10 @@ import { readFileSync } from 'node:fs'; -//const inputArray = readFileSync('sample.txt').toString().split(","); +// const inputArray = readFileSync('sample.txt').toString().split(","); const inputArray = readFileSync('input.txt').toString().split(","); // Part One - +console.time("part1"); let sum = 0; for (const element of inputArray) { @@ -22,17 +22,44 @@ for (const element of inputArray) { } } } - +console.timeEnd("part1"); console.log(sum); // Part Two +console.time("part2"); +let sum2 = 0; +const repeats = []; + for (const element of inputArray) { const contents = element.split("-"); + const start = Number.parseInt(contents[0]); + const end = Number.parseInt(contents[1]); + for (let sku = start; sku <= end; sku++) { + const skuString = sku.toString(); + const digits = skuString.length + const maxRepeatLength = Math.floor(digits / 2); + for (let repeatLength = 1; repeatLength <= maxRepeatLength; repeatLength++) { + if (digits % repeatLength === 0) { + const maxRepeats = digits / repeatLength; + const repeatPattern = skuString.slice(0, repeatLength); + const repeatedPattern = repeatPattern.repeat(maxRepeats); + if (repeatedPattern === skuString) { + repeats.push(sku); + } + } + } + } } -console.log(); +const uniqueRepeats = [...new Set(repeats)]; + +for (const repeat of uniqueRepeats) { + sum2 += Number.parseInt(repeat); +} +console.timeEnd("part2"); +console.log(sum2); // functions diff --git a/03/code.js b/03/code.js index f8ea725..1e31c19 100644 --- a/03/code.js +++ b/03/code.js @@ -4,22 +4,23 @@ const inputArray = readFileSync('sample.txt').toString().split("\n"); // const inputArray = readFileSync('input.txt').toString().split("\n"); // Part One - +console.time("part1"); for (const element of inputArray) { const contents = element.split(" "); } +console.timeEnd("part1"); console.log(); // Part Two - +console.time("part2"); for (const element of inputArray) { const contents = element.split(" "); } console.log(); - +console.timeEnd("part2"); // functions diff --git a/day-template/code.js b/day-template/code.js index f8ea725..54f290d 100644 --- a/day-template/code.js +++ b/day-template/code.js @@ -5,21 +5,24 @@ const inputArray = readFileSync('sample.txt').toString().split("\n"); // Part One +console.time("part1"); for (const element of inputArray) { const contents = element.split(" "); } +console.timeEnd("part1"); console.log(); // Part Two +console.time("part2"); for (const element of inputArray) { const contents = element.split(" "); } console.log(); - +console.timeEnd("part2"); // functions