diff --git a/02/code.js b/02/code.js index f8ea725..68ac12b 100644 --- a/02/code.js +++ b/02/code.js @@ -1,22 +1,35 @@ import { readFileSync } from 'node:fs'; -const inputArray = readFileSync('sample.txt').toString().split("\n"); -// const inputArray = readFileSync('input.txt').toString().split("\n"); +//const inputArray = readFileSync('sample.txt').toString().split(","); +const inputArray = readFileSync('input.txt').toString().split(","); // Part One -for (const element of inputArray) { - const contents = element.split(" "); +let sum = 0; +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 + if (digits % 2 === 0) { + if (skuString.slice(0, digits / 2) === skuString.slice(digits / 2)) { + sum += sku; + } + } + } } -console.log(); +console.log(sum); // Part Two for (const element of inputArray) { - const contents = element.split(" "); + const contents = element.split("-"); }