|
| 1 | +import run from "aocrunner"; |
| 2 | + |
| 3 | +import { MinHeap } from "../utils/index.js"; |
| 4 | + |
| 5 | +interface Direction { |
| 6 | + dx: number; |
| 7 | + dy: number; |
| 8 | +} |
| 9 | + |
| 10 | +const allDirections: Direction[] = [ |
| 11 | + { dx: -1, dy: 0 }, |
| 12 | + { dx: 1, dy: 0 }, |
| 13 | + { dx: 0, dy: -1 }, |
| 14 | + { dx: 0, dy: 1 }, |
| 15 | +]; |
| 16 | + |
| 17 | +interface Node { |
| 18 | + x: number; |
| 19 | + y: number; |
| 20 | + steps: number; |
| 21 | +} |
| 22 | + |
| 23 | +const parseInput = (rawInput: string) => |
| 24 | + rawInput.split("\n").map((line) => line.split(",").map((n) => parseInt(n))); |
| 25 | + |
| 26 | +const makeKey = (node: Node) => `${node.x}|${node.y}`; |
| 27 | + |
| 28 | +const canPath = (grid: number[][], endX: number, endY: number) => { |
| 29 | + let visited = new Set<string>(); |
| 30 | + const hasVisited = (node: Node) => { |
| 31 | + const key = makeKey(node); |
| 32 | + if (visited.has(key)) return true; |
| 33 | + visited.add(key); |
| 34 | + return false; |
| 35 | + }; |
| 36 | + |
| 37 | + let pointsToVisit: Node[] = []; |
| 38 | + MinHeap.push(pointsToVisit, [0, { x: 0, y: 0, steps: 0 }]); |
| 39 | + |
| 40 | + while (pointsToVisit.length > 0) { |
| 41 | + let [_, node] = MinHeap.pop(pointsToVisit) as [number, Node]; |
| 42 | + |
| 43 | + if (hasVisited(node)) continue; |
| 44 | + if (node.y === endY && node.x === endX) { |
| 45 | + return node.steps; |
| 46 | + } |
| 47 | + allDirections.forEach((d) => { |
| 48 | + const nextX = node.x + d.dx; |
| 49 | + const nextY = node.y + d.dy; |
| 50 | + |
| 51 | + if ( |
| 52 | + nextY < 0 || |
| 53 | + nextY >= grid.length || |
| 54 | + nextX < 0 || |
| 55 | + nextX >= grid[0].length |
| 56 | + ) |
| 57 | + return; |
| 58 | + |
| 59 | + if (grid[nextY][nextX] === 1) return; |
| 60 | + |
| 61 | + MinHeap.push(pointsToVisit, [ |
| 62 | + node.steps + 1, |
| 63 | + { |
| 64 | + x: nextX, |
| 65 | + y: nextY, |
| 66 | + direction: d, |
| 67 | + steps: node.steps + 1, |
| 68 | + }, |
| 69 | + ]); |
| 70 | + }); |
| 71 | + } |
| 72 | + return -1; |
| 73 | +}; |
| 74 | + |
| 75 | +const part1 = (rawInput: string) => { |
| 76 | + const input = parseInput(rawInput); |
| 77 | + |
| 78 | + const sampleData = input.length < 30; |
| 79 | + const endX = input.length < 30 ? 6 : 70; |
| 80 | + const endY = input.length < 30 ? 6 : 70; |
| 81 | + const grid = new Array(endY + 1) |
| 82 | + .fill(0) |
| 83 | + .map(() => new Array<number>(endX + 1).fill(0)); |
| 84 | + |
| 85 | + input.forEach(([x, y], i) => { |
| 86 | + if (i < (sampleData ? 12 : 1024)) { |
| 87 | + grid[y][x] = 1; |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + return canPath(grid, endX, endY); |
| 92 | +}; |
| 93 | + |
| 94 | +const part2 = (rawInput: string) => { |
| 95 | + const input = parseInput(rawInput); |
| 96 | + |
| 97 | + const sampleData = input.length < 30; |
| 98 | + const endX = input.length < 30 ? 6 : 70; |
| 99 | + const endY = input.length < 30 ? 6 : 70; |
| 100 | + const grid = new Array(endY + 1) |
| 101 | + .fill(0) |
| 102 | + .map(() => new Array<number>(endX + 1).fill(0)); |
| 103 | + |
| 104 | + for (let i = 0; i < input.length; i++) { |
| 105 | + let [x, y] = input[i]; |
| 106 | + if (i < (sampleData ? 12 : 2979)) { |
| 107 | + grid[y][x] = 1; |
| 108 | + } else { |
| 109 | + grid[y][x] = 1; |
| 110 | + if (canPath(grid, endX, endY) < 0) { |
| 111 | + return `${x},${y}`; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return "passable?"; |
| 117 | +}; |
| 118 | + |
| 119 | +run({ |
| 120 | + part1: { |
| 121 | + tests: [ |
| 122 | + { |
| 123 | + input: `5,4 |
| 124 | +4,2 |
| 125 | +4,5 |
| 126 | +3,0 |
| 127 | +2,1 |
| 128 | +6,3 |
| 129 | +2,4 |
| 130 | +1,5 |
| 131 | +0,6 |
| 132 | +3,3 |
| 133 | +2,6 |
| 134 | +5,1 |
| 135 | +1,2 |
| 136 | +5,5 |
| 137 | +2,5 |
| 138 | +6,5 |
| 139 | +1,4 |
| 140 | +0,4 |
| 141 | +6,4 |
| 142 | +1,1 |
| 143 | +6,1 |
| 144 | +1,0 |
| 145 | +0,5 |
| 146 | +1,6 |
| 147 | +2,0`, |
| 148 | + expected: 22, |
| 149 | + }, |
| 150 | + ], |
| 151 | + solution: part1, |
| 152 | + }, |
| 153 | + part2: { |
| 154 | + tests: [ |
| 155 | + // { |
| 156 | + // input: ``, |
| 157 | + // expected: "", |
| 158 | + // }, |
| 159 | + ], |
| 160 | + solution: part2, |
| 161 | + }, |
| 162 | + trimTestInputs: true, |
| 163 | + onlyTests: false, |
| 164 | +}); |
0 commit comments