|
| 1 | +import { readFile, writeFile } from 'node:fs/promises'; |
| 2 | + |
| 3 | +import { NumericGrid, CharGrid } from './core/grid.mjs'; |
| 4 | +import { modulo } from './core/math.mjs'; |
| 5 | +import { Stopwatch } from './core/performance.mjs'; |
| 6 | +import { getDataPath } from './core/project.mjs'; |
| 7 | + |
| 8 | +export async function solvePuzzleBasic() { |
| 9 | + const content = await readFile(getDataPath('input/puzzle14.txt'), {encoding: 'utf8'}); |
| 10 | + const robots = parseRobots(content); |
| 11 | + |
| 12 | + // const room = NumericGrid.empty(7, 11); |
| 13 | + const room = NumericGrid.empty(103, 101); |
| 14 | + |
| 15 | + const roomSize: Vector = [room.columns, room.rows]; |
| 16 | + for (const robot of robots) { |
| 17 | + const [x, y] = robotPositionAfterTime(robot, 100, roomSize); |
| 18 | + room.set(y, x, room.get(y, x) + 1); |
| 19 | + } |
| 20 | + |
| 21 | + await writeFile( |
| 22 | + getDataPath('output/puzzle14_after100.txt'), |
| 23 | + Array.from(CharGrid.from(room, countToChar).lines()).join(''), |
| 24 | + {encoding: 'utf8'} |
| 25 | + ); |
| 26 | + |
| 27 | + const factor = computeSafetyFactor(room); |
| 28 | + console.log(`Puzzle 14 (basic): ${factor}`); |
| 29 | +} |
| 30 | + |
| 31 | +export async function solvePuzzleAdvanced() { |
| 32 | + const content = await readFile(getDataPath('input/puzzle14.txt'), {encoding: 'utf8'}); |
| 33 | + const robots = parseRobots(content); |
| 34 | + |
| 35 | + const room = NumericGrid.empty(103, 101); |
| 36 | + |
| 37 | + const roomSize: Vector = [room.columns, room.rows]; |
| 38 | + const factors: number[] = []; |
| 39 | + let targetTime: number | undefined; |
| 40 | + for (let i = 0; i < 10_000; i++) { |
| 41 | + room.fill(0); |
| 42 | + for (const robot of robots) { |
| 43 | + const [x, y] = robotPositionAfterTime(robot, i, roomSize); |
| 44 | + room.set(y, x, room.get(y, x) + 1); |
| 45 | + } |
| 46 | + const factor = computeSafetyFactor(room); |
| 47 | + factors.push(factor); |
| 48 | + if (targetTime === undefined && factor < 50_000_000) { |
| 49 | + targetTime = i; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + await writeFile( |
| 54 | + getDataPath('output/puzzle14_factors.txt'), |
| 55 | + factors.map(f => String(f)).join('\n'), |
| 56 | + {encoding: 'utf8'} |
| 57 | + ); |
| 58 | + |
| 59 | + if (targetTime !== undefined) { |
| 60 | + room.fill(0); |
| 61 | + for (const robot of robots) { |
| 62 | + const [x, y] = robotPositionAfterTime(robot, targetTime, roomSize); |
| 63 | + room.set(y, x, room.get(y, x) + 1); |
| 64 | + } |
| 65 | + await writeFile( |
| 66 | + getDataPath('output/puzzle14_easterEgg.txt'), |
| 67 | + Array.from(CharGrid.from(room, countToChar).lines()).join(''), |
| 68 | + {encoding: 'utf8'} |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + console.log(`Puzzle 14 (advanced): ${targetTime}`); |
| 73 | +} |
| 74 | + |
| 75 | +type Vector = readonly [x: number, y: number]; |
| 76 | + |
| 77 | +interface Robot { |
| 78 | + readonly index: number; |
| 79 | + readonly position: Vector; |
| 80 | + readonly velocity: Vector; |
| 81 | +} |
| 82 | + |
| 83 | +function parseRobots(content: string): Robot[] { |
| 84 | + const result: Robot[] = []; |
| 85 | + |
| 86 | + for (const line of content.split(/\r?\n/)) { |
| 87 | + if (!line) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + const match = /^p=(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)$/.exec(line); |
| 91 | + if (!match) { |
| 92 | + throw new Error(`Invalid robot line: ${line}`); |
| 93 | + } |
| 94 | + const [, x, y, vx, vy] = Array.from(match, n => Number(n)); |
| 95 | + result.push({ |
| 96 | + index: result.length, |
| 97 | + position: [x, y], |
| 98 | + velocity: [vx, vy], |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + return result; |
| 103 | +} |
| 104 | + |
| 105 | +function robotPositionAfterTime(robot: Robot, seconds: number, roomSize: Vector): Vector { |
| 106 | + let [x, y] = robot.position; |
| 107 | + const [vx, vy] = robot.velocity; |
| 108 | + const [width, height] = roomSize; |
| 109 | + const rx = modulo(x + vx * seconds, width); |
| 110 | + const ry = modulo(y + vy * seconds, height); |
| 111 | + return [rx, ry]; |
| 112 | +} |
| 113 | + |
| 114 | +function computeSafetyFactor(room: NumericGrid): number { |
| 115 | + const halfRow = Math.floor(room.rows / 2); |
| 116 | + const halfColumn = Math.floor(room.columns / 2); |
| 117 | + let totalTL = 0; |
| 118 | + let totalTR = 0; |
| 119 | + let totalBL = 0; |
| 120 | + let totalBR = 0; |
| 121 | + for (let i = 0; i < room.rows; i++) { |
| 122 | + for (let j = 0; j < room.columns; j++) { |
| 123 | + const count = room.get(i, j); |
| 124 | + if (i < halfRow) { |
| 125 | + if (j < halfColumn) { |
| 126 | + totalTL += count; |
| 127 | + } else if (j > halfColumn) { |
| 128 | + totalBL += count; |
| 129 | + } |
| 130 | + } else if (i > halfRow) { |
| 131 | + if (j < halfColumn) { |
| 132 | + totalTR += count; |
| 133 | + } else if (j > halfColumn) { |
| 134 | + totalBR += count; |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + return totalTL * totalTR * totalBL * totalBR; |
| 140 | +} |
| 141 | + |
| 142 | +const COUNT_TO_CHAR = '.123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 143 | + |
| 144 | +function countToChar(n: number): string { |
| 145 | + return COUNT_TO_CHAR[n] ?? '!'; |
| 146 | +} |
| 147 | + |
| 148 | +(async function main() { |
| 149 | + using _ = Stopwatch.start(); |
| 150 | + await solvePuzzleBasic(); |
| 151 | + await solvePuzzleAdvanced(); |
| 152 | +})(); |
0 commit comments