|
| 1 | +import { timePart1, timePart2 } from "../../utils/time-part"; |
| 2 | + |
| 3 | +const parseInput = (input: string) => { |
| 4 | + return input.split("\n").map((row) => row.split("")); |
| 5 | +}; |
| 6 | + |
| 7 | +type InputMap = ReturnType<typeof parseInput>; |
| 8 | + |
| 9 | +const findCell = (char: string, map: InputMap) => { |
| 10 | + const y = map.findIndex((row) => row.includes(char)); |
| 11 | + const x = map[y].indexOf(char); |
| 12 | + |
| 13 | + return { x, y }; |
| 14 | +}; |
| 15 | + |
| 16 | +const getStartingPosition = (map: InputMap) => findCell("S", map); |
| 17 | +const getEndPosition = (map: InputMap) => findCell("E", map); |
| 18 | + |
| 19 | +const DIRECTIONS = ["N", "E", "S", "W"] as const; |
| 20 | +type Direction = (typeof DIRECTIONS)[number]; |
| 21 | + |
| 22 | +type Coordinate = { x: number; y: number }; |
| 23 | + |
| 24 | +const positionToKey = (position: Coordinate, direction?: Direction) => { |
| 25 | + const positionKey = `${position.x},${position.y}`; |
| 26 | + |
| 27 | + if (direction) { |
| 28 | + return `${positionKey}:${direction}`; |
| 29 | + } |
| 30 | + |
| 31 | + return positionKey; |
| 32 | +}; |
| 33 | + |
| 34 | +const rotate = ( |
| 35 | + direction: Direction, |
| 36 | + { clockwise } = { clockwise: true } |
| 37 | +): Direction => { |
| 38 | + if (direction === "N" && !clockwise) { |
| 39 | + return "W"; |
| 40 | + } |
| 41 | + |
| 42 | + return DIRECTIONS[ |
| 43 | + (DIRECTIONS.indexOf(direction) + (clockwise ? 1 : -1)) % DIRECTIONS.length |
| 44 | + ]; |
| 45 | +}; |
| 46 | + |
| 47 | +const MOVEMENTS = { |
| 48 | + N: { x: 0, y: -1 }, |
| 49 | + E: { x: 1, y: 0 }, |
| 50 | + S: { x: 0, y: 1 }, |
| 51 | + W: { x: -1, y: 0 }, |
| 52 | +} satisfies Record<Direction, Coordinate>; |
| 53 | + |
| 54 | +const move = (position: Coordinate, direction: Direction) => { |
| 55 | + const movement = MOVEMENTS[direction]; |
| 56 | + |
| 57 | + return { x: position.x + movement.x, y: position.y + movement.y }; |
| 58 | +}; |
| 59 | + |
| 60 | +const readCell = ({ x, y }: Coordinate, map: InputMap) => map[y]?.[x]; |
| 61 | + |
| 62 | +type QueueEntry = { |
| 63 | + direction: Direction; |
| 64 | + position: Coordinate; |
| 65 | + score: number; |
| 66 | + visited: Coordinate[]; |
| 67 | +}; |
| 68 | + |
| 69 | +const getBestScore = (map: InputMap) => { |
| 70 | + const start = getStartingPosition(map); |
| 71 | + const end = getEndPosition(map); |
| 72 | + |
| 73 | + const queue: QueueEntry[] = [ |
| 74 | + { |
| 75 | + direction: "E", |
| 76 | + position: start, |
| 77 | + score: 0, |
| 78 | + visited: [start], |
| 79 | + }, |
| 80 | + ]; |
| 81 | + |
| 82 | + const globalVisited = new Set<string>(); |
| 83 | + let bestScore: number | null = null; |
| 84 | + |
| 85 | + while (queue.length) { |
| 86 | + queue.sort((a, b) => a.score - b.score); |
| 87 | + |
| 88 | + const route = queue.shift()!; |
| 89 | + const baseKey = positionToKey(route.position, route.direction); |
| 90 | + |
| 91 | + if (globalVisited.has(baseKey)) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + globalVisited.add(baseKey); |
| 95 | + |
| 96 | + if (route.position.x === end.x && route.position.y === end.y) { |
| 97 | + if (typeof bestScore !== "number" || route.score < bestScore) { |
| 98 | + bestScore = route.score; |
| 99 | + } |
| 100 | + |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + const movedForward = move(route.position, route.direction); |
| 105 | + const cell = readCell(movedForward, map); |
| 106 | + |
| 107 | + if (cell !== "#") { |
| 108 | + queue.push({ |
| 109 | + ...route, |
| 110 | + position: movedForward, |
| 111 | + score: route.score + 1, |
| 112 | + visited: [...route.visited, movedForward], |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + queue.push({ |
| 117 | + ...route, |
| 118 | + direction: rotate(route.direction), |
| 119 | + score: route.score + 1000, |
| 120 | + visited: [...route.visited], |
| 121 | + }); |
| 122 | + |
| 123 | + queue.push({ |
| 124 | + ...route, |
| 125 | + direction: rotate(route.direction, { clockwise: false }), |
| 126 | + score: route.score + 1000, |
| 127 | + visited: [...route.visited], |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + return bestScore ?? 0; |
| 132 | +}; |
| 133 | + |
| 134 | +export const part1 = timePart1((input: string) => { |
| 135 | + const map = parseInput(input); |
| 136 | + const bestScore = getBestScore(map); |
| 137 | + |
| 138 | + return bestScore; |
| 139 | +}); |
| 140 | + |
| 141 | +export const part2 = timePart2((input: string) => { |
| 142 | + const map = parseInput(input); |
| 143 | + const bestScore = getBestScore(map); |
| 144 | + const startingPosition = getStartingPosition(map); |
| 145 | + const endPosition = getEndPosition(map); |
| 146 | + |
| 147 | + const queue: QueueEntry[] = [ |
| 148 | + { |
| 149 | + visited: [startingPosition], |
| 150 | + position: startingPosition, |
| 151 | + direction: "E", |
| 152 | + score: 0, |
| 153 | + }, |
| 154 | + ]; |
| 155 | + |
| 156 | + const globalVisited = new Map<string, number>(); |
| 157 | + const pathsToEnd: QueueEntry["visited"][] = []; |
| 158 | + |
| 159 | + while (queue.length) { |
| 160 | + const route = queue.shift()!; |
| 161 | + const baseKey = positionToKey(route.position, route.direction); |
| 162 | + |
| 163 | + if ( |
| 164 | + route.score > bestScore || |
| 165 | + (globalVisited.has(baseKey) && globalVisited.get(baseKey)! < route.score) |
| 166 | + ) { |
| 167 | + continue; |
| 168 | + } |
| 169 | + |
| 170 | + globalVisited.set(baseKey, route.score); |
| 171 | + |
| 172 | + if ( |
| 173 | + route.position.x === endPosition.x && |
| 174 | + route.position.y === endPosition.y && |
| 175 | + route.score === bestScore |
| 176 | + ) { |
| 177 | + pathsToEnd.push(route.visited); |
| 178 | + continue; |
| 179 | + } |
| 180 | + |
| 181 | + const movedForward = move(route.position, route.direction); |
| 182 | + const cell = readCell(movedForward, map); |
| 183 | + |
| 184 | + if (cell !== "#") { |
| 185 | + queue.push({ |
| 186 | + ...route, |
| 187 | + position: movedForward, |
| 188 | + score: route.score + 1, |
| 189 | + visited: [...route.visited, movedForward], |
| 190 | + }); |
| 191 | + } |
| 192 | + |
| 193 | + queue.push({ |
| 194 | + ...route, |
| 195 | + direction: rotate(route.direction), |
| 196 | + score: route.score + 1000, |
| 197 | + visited: [...route.visited], |
| 198 | + }); |
| 199 | + |
| 200 | + queue.push({ |
| 201 | + ...route, |
| 202 | + direction: rotate(route.direction, { clockwise: false }), |
| 203 | + score: route.score + 1000, |
| 204 | + visited: [...route.visited], |
| 205 | + }); |
| 206 | + } |
| 207 | + |
| 208 | + const bestSeats = new Set<string>(); |
| 209 | + |
| 210 | + for (const pathToGoal of pathsToEnd) { |
| 211 | + for (const position of pathToGoal) { |
| 212 | + const key = positionToKey(position); |
| 213 | + bestSeats.add(key); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + return bestSeats.size; |
| 218 | +}); |
0 commit comments