advent-of-code

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

commit 18d7a907395ad330b2af78d140141623d725d112
parent a84967f32c1bac4613bb244d9536697308424c80
Author: finwo <finwo@pm.me>
Date:   Sun, 28 Dec 2025 11:37:24 +0100

Completed 2025/08

Diffstat:
A2025/08/example.txt | 20++++++++++++++++++++
A2025/08/index.js | 173+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 193 insertions(+), 0 deletions(-)

diff --git a/2025/08/example.txt b/2025/08/example.txt @@ -0,0 +1,20 @@ +162,817,812 +57,618,57 +906,360,560 +592,479,940 +352,342,300 +466,668,158 +542,29,236 +431,825,988 +739,650,466 +52,470,668 +216,146,977 +819,987,18 +117,168,530 +805,96,715 +346,949,466 +970,615,88 +941,993,340 +862,61,35 +984,92,344 +425,690,689 diff --git a/2025/08/index.js b/2025/08/index.js @@ -0,0 +1,173 @@ +#!/usr/bin/env node + +const fs = require('node:fs'); + +String.prototype.splice = function (start, deleteCount, ...newElements) { + return this.slice(0, start) + `${(newElements||[]).join('')}` + this.slice(start + deleteCount); +} + +const connectionlimit = 10; + +const input = fs + .readFileSync('input', 'utf-8') + .split('\r\n').join('\n') + .split('\r').join('\n') + .split('\n') + .filter(e => e) + .map(line => line.split(',').map(n => parseInt(n,10))) + +const part1circuits = []; +const part1map = []; +const part1boxes = input.map(pos => ({ pos, circuit: null })) + +// Map out all possible connections +for(let idx_src = 0 ; idx_src < input.length ; idx_src++) { + for(let idx_dst = idx_src + 1 ; idx_dst < input.length ; idx_dst++) { + const ref = { + key: `${idx_src}:${idx_dst}`, + src: part1boxes[idx_src], + dst: part1boxes[idx_dst], + }; + ref.distance = Math.sqrt( + ((ref.src.pos[0]-ref.dst.pos[0])**2) + + ((ref.src.pos[1]-ref.dst.pos[1])**2) + + ((ref.src.pos[2]-ref.dst.pos[2])**2) + ); + part1map.push(ref); + } +} + +// Sort all possible connections +part1map.sort((a,b) => a.distance - b.distance); + +// Let's lay the ones we need +let part1connections = 0; + +let part2_lastsrc = null; +let part2_lastdst = null; + +for(let candidate of part1map) { + + // Print out part1 when limit reached + if (part1connections == connectionlimit) { + // Sort circuits by size + part1circuits.sort((a,b) => b.length - a.length); + + const part1 = part1circuits + .slice(0,3) + .map(circuit => circuit.length) + .reduce((r,a) => r*a, 1) + ; + process.stdout.write(`------[ Part 1: ${part1} ]------\n`); + } + + // Neither src/dst in circuit = new circuit + if ( + (!candidate.src.circuit) && + (!candidate.dst.circuit) + ) { + part2_lastsrc = candidate.src; + part2_lastdst = candidate.dst; + const circuit = []; + part1circuits.push(circuit); + candidate.src.circuit = circuit; + candidate.dst.circuit = circuit; + circuit.push(candidate.src); + circuit.push(candidate.dst); + part1connections++; + continue; + } + + // src has circuit, dst has no circuit + if ( + ( candidate.src.circuit) && + (!candidate.dst.circuit) + ) { + part2_lastsrc = candidate.src; + part2_lastdst = candidate.dst; + candidate.dst.circuit = candidate.src.circuit; + candidate.src.circuit.push(candidate.dst); + part1connections++; + continue; + } + + // dst has circuit, src has no circuit + if ( + ( candidate.dst.circuit) && + (!candidate.src.circuit) + ) { + part2_lastsrc = candidate.src; + part2_lastdst = candidate.dst; + candidate.src.circuit = candidate.dst.circuit; + candidate.dst.circuit.push(candidate.src); + part1connections++; + continue; + } + + // Both have circuits, NOT the same circuit + if ( + ( candidate.src.circuit) && + ( candidate.dst.circuit) && + ( candidate.src.circuit !== candidate.dst.circuit ) + ) { + part2_lastsrc = candidate.src; + part2_lastdst = candidate.dst; + const circuit = []; + const circuit_src = candidate.src.circuit; + const circuit_dst = candidate.dst.circuit; + for(const box of candidate.src.circuit) { + box.circuit = circuit; + circuit.push(box); + } + for(const box of candidate.dst.circuit) { + box.circuit = circuit; + circuit.push(box); + } + part1circuits.push(circuit); + part1circuits.splice(part1circuits.indexOf(circuit_src), 1); + part1circuits.splice(part1circuits.indexOf(circuit_dst), 1); + part1connections++; + continue; + } + + // Both have circuits, within same circuit + if ( + ( candidate.src.circuit) && + ( candidate.dst.circuit) && + ( candidate.src.circuit === candidate.dst.circuit ) + ) { + // Do nothing + part1connections++; + continue; + } + + // console.log(candidate); + throw new Error("Unhandled case"); +} + +const part2 = part2_lastsrc.pos[0] * part2_lastdst.pos[0]; + +process.stdout.write(`------[ Part 2: ${part2} ]------\n`); + +// console.log(part1circuits); +// console.log({ +// part2_lastsrc, +// part2_lastdst +// }); + +// // Sort circuits by size +// part1circuits.sort((a,b) => b.length - a.length); + +// const part1 = part1circuits +// .slice(0,3) +// .map(circuit => circuit.length) +// .reduce((r,a) => r*a, 1) +// ; + + + + + + + +// process.stdout.write(`------[ Part 1: ${part1} ]------\n`);