advent-of-code

Entries to advent of code, multiple years
git clone git://git.finwo.net/misc/advent-of-code
Log | Files | Refs

commit ac53b953ffdc622dce5c6079768ef9aaf91b9329
parent af593b517b9ab70e87e6f987cb9ca13b23d0c437
Author: finwo <finwo@pm.me>
Date:   Wed, 24 Dec 2025 21:39:58 +0100

Completed 2025/06

Diffstat:
A2025/06/README.md | 17+++++++++++++++++
A2025/06/example.txt | 5+++++
A2025/06/index.js | 200+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 222 insertions(+), 0 deletions(-)

diff --git a/2025/06/README.md b/2025/06/README.md @@ -0,0 +1,17 @@ +## Hello World + +This is a thing + + +```js +const hello = 'world'; +``` + +```sh +export PATH="dinges" +``` + + + + + diff --git a/2025/06/example.txt b/2025/06/example.txt @@ -0,0 +1,5 @@ +123 328 51 64 + 45 64 387 23 + 6 98 215 314 +* + * + + diff --git a/2025/06/index.js b/2025/06/index.js @@ -0,0 +1,200 @@ +#!/usr/bin/env node + +const fs = require('node:fs'); + +const part1Entries = fs + .readFileSync('input', 'utf-8') + .split('\r\n').join('\n') + .split('\r').join('\n') + .split('\n') + .filter(e => e) + .map(line => line.split(' ').filter(e => e)) + +// Rotate the input +const part1problems = []; +for(let componentIndex = 0 ; componentIndex < part1Entries.length; componentIndex++) { + const row = part1Entries[componentIndex]; + for(let problemIndex = 0 ; problemIndex < row.length ; problemIndex++) { + part1problems[problemIndex]||={ components: [] }; + part1problems[problemIndex].components[componentIndex] = row[problemIndex]; + } +} +// Separate the operator & components +for(const problem of part1problems) { + problem.operator = problem.components.pop(); + problem.components = problem.components.map(BigInt); +} + +const ops = { + '*': (a,b) => a*b, + '+': (a,b) => a+b, +}; + + +// Do math +for(const problem of part1problems) { + const start = problem.operator === '*' ? 1n : 0n; + problem.result = problem.components.reduce(ops[problem.operator], start); + console.log(`${problem.components.join(` ${problem.operator} `)} = ${problem.result}`); +} + +// Sum the results +const part1 = part1problems.reduce((r,a) => r+a.result, 0n); + +const part2raw = fs + .readFileSync('input', 'utf-8') + .split('\r\n').join('\n') + .split('\r').join('\n') + .split('\n') + .filter(e => e) + .map(line => line.split('')) + +const part2rotated = []; +for(let y = 0 ; y < part2raw.length ; y++) { + const line = part2raw[y]; + for(let x = 0 ; x < line.length ; x++) { + part2rotated[x] ||= new Array(part2raw.length).fill(' ').map(_=>' '); + part2rotated[x][y] = line[x]; + } +} + +const part2problems = []; +let buf = []; + +function buf2problem(buf) { + return { + lines: buf.map(line => line.join('')), + operator: buf[0].slice(-1)[0], + components: buf.map(line => BigInt(line.slice(0,-1).join(''))) + }; +} + +for(let line of part2rotated) { + if (line.join('').trim() === '') { + part2problems.push(buf2problem(buf)); + buf = []; + } else { + buf.push(line); + } +} +if (buf.length) { + part2problems.push(buf2problem(buf)); +} + +// Do math +for(const problem of part2problems) { + const start = problem.operator === '*' ? 1n : 0n; + problem.result = problem.components.reduce(ops[problem.operator], start); + console.log(`${problem.components.join(` ${problem.operator} `)} = ${problem.result}`); +} + +// Sum the results +const part2 = part2problems.reduce((r,a) => r+a.result, 0n); + +// // Rotate the input +// const part1problems = []; +// for(let componentIndex = 0 ; componentIndex < part1Entries.length; componentIndex++) { +// const row = part1Entries[componentIndex]; +// for(let problemIndex = 0 ; problemIndex < row.length ; problemIndex++) { +// part1problems[problemIndex]||={ components: [] }; +// part1problems[problemIndex].components[componentIndex] = row[problemIndex]; +// } +// } +// // Separate the operator & components +// for(const problem of part1problems) { +// problem.operator = problem.components.pop(); +// problem.components = problem.components.map(BigInt); +// } + + +console.log(part2problems); + +process.stdout.write(`------[ Part 1: ${part1} ]------\n`); +process.stdout.write(`------[ Part 2: ${part2} ]------\n`); + + +// console.log(JSON.stringify({ rawEntries, problems }, null, 2)); + + +// const ranges = rawRanges +// .split('\n') +// .filter(line => line) +// .map(line => line.split('-').map(n => BigInt(n))) +// .map(range => ({ start: range[0], end: range[1] })) +// ; +// +// const queries = rawQueries +// .split('\n') +// .filter(line => line) +// .map(line => BigInt(line)) +// ; +// +// const compositeRanges = ranges.map(range => ({...range})); +// +// for(let i = 0; i < compositeRanges.length; i++) { +// const range = compositeRanges[i]; +// +// // Find the first range this low one matches +// const foundLow = compositeRanges.find(({start,end}) => { +// if (start <= range.start && range.start <= end) return true; +// if (start <= range.end && range.end <= end) return true; +// if (range.start <= start && start <= range.end) return true; +// if (range.start <= end && end <= range.end) return true; +// return false; +// }); +// const foundLowIndex = compositeRanges.indexOf(foundLow); +// +// // Merge with low range +// if (i !== foundLowIndex) { +// // process.stdout.write(`Merging ${foundLow.start}-${foundLow.end},${range.start}-${range.end} => `); +// range.start = foundLow.start < range.start ? foundLow.start : range.start; +// range.end = foundLow.end > range.end ? foundLow.end : range.end ; +// // process.stdout.write(`${range.start}-${range.end}\n`); +// compositeRanges.splice(foundLowIndex, 1); +// i = -1; +// continue; +// } +// +// // Find the first range this high one matches +// const foundHigh = compositeRanges.find(({start,end}) => { +// if (start <= range.start && range.start <= end) return true; +// if (start <= range.end && range.end <= end) return true; +// if (range.start <= start && start <= range.end) return true; +// if (range.start <= end && end <= range.end) return true; +// return false; +// }); +// const foundHighIndex = compositeRanges.indexOf(foundHigh); +// +// // Merge with high range +// if (i !== foundHighIndex) { +// process.stdout.write(`Merging ${foundHigh.start}-${foundHigh.end},${range.start}-${range.end} => `); +// range.start = foundHigh.start < range.start ? foundHigh.start : range.start; +// range.end = foundHigh.end > range.end ? foundHigh.end : range.end ; +// process.stdout.write(`${range.start}-${range.end}\n`); +// compositeRanges.splice(foundHighIndex, 1); +// i = -1; +// continue; +// } +// } +// +// const part1 = queries +// .filter(ingredientId => compositeRanges.find(({start,end}) => { +// if (ingredientId < start) return false; +// if (ingredientId > end ) return false; +// return true; +// })) +// .length; +// +// const part2 = compositeRanges +// // .map(range => `${range.start}-${range.end} => ${range.end - range.start + 1n}`) +// .map(range => (range.end - range.start + 1n)) +// .reduce((r,a) => r+a, 0n) +// +// console.log(compositeRanges.map(range => `${range.start}-${range.end} => ${range.end - range.start + 1n}`)); +// console.log(compositeRanges.find(range => range.start > range.end)); +// +// // console.log(part2.join('\n')); +// +// process.stdout.write(`------[ Part 1: ${part1} ]------\n`); +// process.stdout.write(`------[ Part 2: ${part2} ]------\n`); +//