|
| 1 | +const shapes = [ |
| 2 | + Object.assign([[0, 0], [1, 0], [2, 0], [3, 0]], { width: 4 }), |
| 3 | + Object.assign([[1, 0], [0, 1], [1, 1], [2, 1], [1, 2]], { width: 3 }), |
| 4 | + Object.assign([[0, 0], [1, 0], [2, 0], [2, 1], [2, 2]], { width: 3 }), |
| 5 | + Object.assign([[0, 0], [0, 1], [0, 2], [0, 3]], { width: 1 }), |
| 6 | + Object.assign([[0, 0], [1, 0], [0, 1], [1, 1]], { width: 2 }), |
| 7 | +]; |
| 8 | + |
| 9 | +const pushCharToDirection: Record<string, number> = { "<": -1, ">": 1 }; |
| 10 | + |
| 11 | +export default function solve(input: string) { |
| 12 | + const chamber: boolean[][] = []; |
| 13 | + for (let i = 0, j = 0; i < 2022; i++) { |
| 14 | + const shape = shapes[i % shapes.length]; |
| 15 | + for (let x = 2, y = chamber.length + 3; y >= 0; y--) { |
| 16 | + const dx = pushCharToDirection[input[j++ % input.length]]; |
| 17 | + if ( |
| 18 | + x + dx >= 0 && x + dx + shape.width <= 7 && |
| 19 | + !shape.some(([rx, ry]) => chamber[y + ry]?.[x + rx + dx]) |
| 20 | + ) x += dx; |
| 21 | + if (y === 0 || shape.some(([rx, ry]) => chamber[y + ry - 1]?.[x + rx])) { |
| 22 | + for (const [rx, ry] of shape) { |
| 23 | + chamber[y + ry] ??= Array.from({ length: 7 }, () => false); |
| 24 | + chamber[y + ry][x + rx] = true; |
| 25 | + } |
| 26 | + break; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + return chamber.length; |
| 31 | +} |
0 commit comments