advent-of-code

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

commit 1a8e4c43e5637684036883f481ca802341bb5ba2
parent 3eefdb2273f806d24906a42adde838478009e9ac
Author: finwo <finwo@pm.me>
Date:   Wed, 24 Dec 2025 18:26:51 +0100

2025/05 half-done

Diffstat:
A2025/05/example.txt | 11+++++++++++
A2025/05/index.js | 84+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 95 insertions(+), 0 deletions(-)

diff --git a/2025/05/example.txt b/2025/05/example.txt @@ -0,0 +1,11 @@ +3-5 +10-14 +16-20 +12-18 + +1 +5 +8 +11 +17 +32 diff --git a/2025/05/index.js b/2025/05/index.js @@ -0,0 +1,84 @@ +#!/usr/bin/env node + +const fs = require('node:fs'); + +const [rawRanges, rawQueries] = fs + .readFileSync('input', 'utf-8') + .split('\r\n').join('\n') + .split('\r').join('\n') + .split('\n\n'); + +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 (range.start < start) return false; + if (range.start > end ) return false; + return true; + }); + 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 (range.end < start) return false; + if (range.end > end ) return false; + return true; + }); + 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(part2.join('\n')); + +process.stdout.write(`------[ Part 1: ${part1} ]------\n`); +process.stdout.write(`------[ Part 2: ${part2} ]------\n`); +