Created
December 22, 2022 06:48
-
-
Save bluepichu/1a86bc47c81cf8fb570dbdf71bcab4f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Advent, f, fm, chr, ord } from "advent"; | |
| import { Set, Map } from "immutable"; | |
| const { compute, computeCheck } = await Advent({ day: 22 }); | |
| interface Face { | |
| id: number; | |
| x: number; | |
| y: number; | |
| top?: FaceAdj; | |
| right?: FaceAdj; | |
| bottom?: FaceAdj; | |
| left?: FaceAdj; | |
| } | |
| interface FaceAdj { | |
| face: Face; | |
| rot: number; | |
| } | |
| const face1: Face = { | |
| id: 1, | |
| x: 50, | |
| y: 0 | |
| }; | |
| const face2: Face = { | |
| id: 2, | |
| x: 100, | |
| y: 0 | |
| }; | |
| const face3: Face = { | |
| id: 3, | |
| x: 50, | |
| y: 50 | |
| }; | |
| const face4: Face = { | |
| id: 4, | |
| x: 0, | |
| y: 100 | |
| }; | |
| const face5: Face = { | |
| id: 5, | |
| x: 50, | |
| y: 100 | |
| }; | |
| const face6: Face = { | |
| id: 6, | |
| x: 0, | |
| y: 150 | |
| }; | |
| face1.top = { face: face6, rot: 1 }; | |
| face6.left = { face: face1, rot: 3 }; | |
| face1.right = { face: face2, rot: 0 }; | |
| face2.left = { face: face1, rot: 0 }; | |
| face1.bottom = { face: face3, rot: 0 }; | |
| face3.top = { face: face1, rot: 0 }; | |
| face1.left = { face: face4, rot: 2 }; | |
| face4.left = { face: face1, rot: 2 }; | |
| face2.top = { face: face6, rot: 0 }; | |
| face6.bottom = { face: face2, rot: 0 }; | |
| face2.right = { face: face5, rot: 2 }; | |
| face5.right = { face: face2, rot: 2 }; | |
| face2.bottom = { face: face3, rot: 1 }; | |
| face3.right = { face: face2, rot: 3 }; | |
| face3.left = { face: face4, rot: 3 }; | |
| face4.top = { face: face3, rot: 1 }; | |
| face3.bottom = { face: face5, rot: 0 }; | |
| face5.top = { face: face3, rot: 0 }; | |
| face4.right = { face: face5, rot: 0 }; | |
| face5.left = { face: face4, rot: 0 }; | |
| face4.bottom = { face: face6, rot: 0 }; | |
| face6.top = { face: face4, rot: 0 }; | |
| face5.bottom = { face: face6, rot: 1 }; | |
| face6.right = { face: face5, rot: 3 }; | |
| const faces = [face1, face2, face3, face4, face5, face6]; | |
| compute(2, async (input) => { | |
| const data = input.parse(f.nnl(f.str())); | |
| const grid = f.cGrid()(data[0]); | |
| let instrs = data[1]; | |
| let x = grid[0].indexOf("."); | |
| let y = 0; | |
| let dx = 1; | |
| let dy = 0; | |
| for (let i = 0; i < grid.length; i++) { | |
| while (grid[i].length < grid[0].length) { | |
| grid[i].push(" "); | |
| } | |
| } | |
| while (instrs.length > 0) { | |
| let amt = 0; | |
| while (instrs.length > 0 && instrs.charCodeAt(0) >= 0x30 && instrs.charCodeAt(0) <= 0x39) { | |
| amt = amt * 10 + instrs.charCodeAt(0) - 0x30; | |
| instrs = instrs.slice(1); | |
| } | |
| if (amt === 0) { | |
| throw new Error(); | |
| } | |
| console.log("moving", amt); | |
| for (let i = 0; i < amt; i++) { | |
| let newX = x + dx; | |
| let newY = y + dy; | |
| let newDx = dx; | |
| let newDy = dy; | |
| if (Math.floor(x / 50) !== Math.floor(newX / 50) || Math.floor(y / 50) !== Math.floor(newY / 50)) { | |
| // move to new face | |
| let curface = faces.find((f) => f.x <= x && f.x + 50 > x && f.y <= y && f.y + 50 > y)!; | |
| let adj: FaceAdj; | |
| if (dx === 1) { | |
| adj = curface.right!; | |
| } else if (dx === -1) { | |
| adj = curface.left!; | |
| } else if (dy === 1) { | |
| adj = curface.bottom!; | |
| } else if (dy === -1) { | |
| adj = curface.top!; | |
| } else { | |
| throw new Error(); | |
| } | |
| console.log(curface, adj); | |
| let fx = x - curface.x; | |
| let fy = y - curface.y; | |
| for (let i = 0; i < adj.rot; i++) { | |
| let tmp = fx; | |
| fx = 49 - fy; | |
| fy = tmp; | |
| tmp = newDx; | |
| newDx = -newDy; | |
| newDy = tmp; | |
| } | |
| fx = (fx + newDx + 50) % 50; | |
| fy = (fy + newDy + 50) % 50; | |
| newX = adj.face.x + fx; | |
| newY = adj.face.y + fy; | |
| } | |
| if (grid[newY][newX] === ".") { | |
| x = newX; | |
| y = newY; | |
| dx = newDx; | |
| dy = newDy; | |
| console.log("->", x, y); | |
| } else { | |
| console.log("hit a wall", grid[newY][newX].charCodeAt(0)); | |
| break; | |
| } | |
| } | |
| if (instrs.length > 0) { | |
| switch (instrs[0]) { | |
| case "L": { | |
| let tmp = dx; | |
| dx = dy; | |
| dy = -tmp; | |
| break; | |
| } | |
| case "R": { | |
| let tmp = dx; | |
| dx = -dy; | |
| dy = tmp; | |
| break; | |
| } | |
| } | |
| console.log("rot", instrs[0], dx, dy); | |
| instrs = instrs.slice(1); | |
| } | |
| } | |
| let ans = (y+1) * 1000 + (x+1) * 4; | |
| if (dx == 1 && dy == 0) { | |
| ans += 0; | |
| } else if (dx == 0 && dy == 1) { | |
| ans += 1; | |
| } else if (dx == -1 && dy == 0) { | |
| ans += 2; | |
| } else if (dx == 0 && dy == -1) { | |
| ans += 3; | |
| } | |
| return ans; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment