AoC 2022 D22: Monkey Map
| Problem statement | Source code | Tags: Grid walkingGeometry
← Previous Back to AoC Index Next →
Part 1
There's not much to say about part 1. I thought you need something fancy like precomputing the farthest reachable point in each direction for each cell, but it turns out you can just do the walk naïvely.
The slightly tricky part is handling wraparound. I did a row scan followed by a column scan for the minimum/maximum points in each row/column. Then, when the current position goes out of bounds, I can just look up the corresponding point in the opposite direction.
Wrap around is handled as we move one step:
And then I have moveSteps to move the specified number of steps until either a wall is hit (moveStep returns Nothing) or the whole distance is walked.
Because the direction is an integer from 0 to 3, turning is about as convenient as using complex numbers.
Part 2
OK, this turned out to be really intractable. I had to hardcode the wraparound rules for my input, because I have no idea how to do cube construction generically. I can't even figure out how to do it theoretically for my input; I had to actually get a piece of paper and fold it myself. Like 2020 day 20, I represent each edge of each shape as one of T, L, B, R, and then represent wraparound as edge adjacency of the form (A, R) <-> (D, R').
In the diagram above, the dashed lines are "trans" edges, meaning they connect X to Y'. This means that after edge translation, we also have to reflect about the center of the edge by doing a 50 - coordinate (the face width is 50). I hardcoded all of the exit positions and reenter positions.
Since we have to do hardcoding for part 2 anyway, I also turned my part 1 into a hardcoded lookup table. Now, whenever I reach a state that's immediately outside the grid, I can just look up the corresponding state on the opposite side of the cube.