advent-of-code

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

commit a84967f32c1bac4613bb244d9536697308424c80
parent ac53b953ffdc622dce5c6079768ef9aaf91b9329
Author: finwo <finwo@pm.me>
Date:   Sun, 28 Dec 2025 10:26:23 +0100

Completed 2025/07

Diffstat:
A2025/07/example.txt | 16++++++++++++++++
A2025/07/index.js | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 86 insertions(+), 0 deletions(-)

diff --git a/2025/07/example.txt b/2025/07/example.txt @@ -0,0 +1,16 @@ +.......S....... +............... +.......^....... +............... +......^.^...... +............... +.....^.^.^..... +............... +....^.^...^.... +............... +...^.^...^.^... +............... +..^...^.....^.. +............... +.^.^.^.^.^...^. +............... diff --git a/2025/07/index.js b/2025/07/index.js @@ -0,0 +1,70 @@ +#!/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 input = fs + .readFileSync('input', 'utf-8') + .split('\r\n').join('\n') + .split('\r').join('\n') + .split('\n') + .filter(e => e) + +// Copy & turn into character array +const part1map = [ + ...input.map(line => line.split('')) +]; + +// Copy & turn into character array +const part2map = [ + ...input.map(line => line + .split('') + .map(char => char === 'S' ? 1 : 0) + ) +]; + +let totalSplits = 0; + +for(let lineIdx = 1 ; lineIdx < part1map.length ; lineIdx++) { + for(let charIdx = 0 ; charIdx < part1map[lineIdx].length ; charIdx++) { + + switch(`${part1map[lineIdx-1][charIdx]}${part1map[lineIdx][charIdx]}`) { + case 'S.': + case '|.': + case '||': + part1map[lineIdx][charIdx] = '|'; + part2map[lineIdx][charIdx] += part2map[lineIdx-1][charIdx]; + break; + case '|^': + part1map[lineIdx][charIdx-1] = '|'; + part1map[lineIdx][charIdx+1] = '|'; + part2map[lineIdx][charIdx-1] += part2map[lineIdx-1][charIdx]; + part2map[lineIdx][charIdx+1] += part2map[lineIdx-1][charIdx]; + totalSplits++; + break; + case '^.': + case '.^': + case '.|': + case '..': + // Intentionally empty + break; + default: + throw new Error(`Unhandled case: ${part1map[lineIdx-1][charIdx]}${part1map[lineIdx][charIdx]}`); + } + + } +} + +const part1 = totalSplits; +console.log(`${part1map.map(line => line.join('')).join('\n')}`); +console.log(`${part2map.map(line => line.map(n => n.toString(36)).join('')).join('\n')}`); + +const timelines = part2map.slice(-1)[0].reduce((r,a)=>r+a,0); +const part2 = timelines; + +process.stdout.write(`\n\n`); +process.stdout.write(`------[ Part 1: ${part1} ]------\n`); +process.stdout.write(`------[ Part 2: ${part2} ]------\n`);