Metadata
-
Date
-
Tagged
-
Part of series
- Advent of Code 2022 Day 1
- Advent of Code 2022 Day 2
- Advent of Code 2022 Day 3
- Advent of Code 2022 Day 4
- Advent of Code 2022 Day 5
- Advent of Code 2022 Day 6
- Advent of Code 2022 Day 7
- Advent of Code 2022 Day 8
- Advent of Code 2022 Day 9
- Advent of Code 2022 Day 10
- Advent of Code 2022 Day 11
- Advent of Code 2022 Day 12
- Advent of Code 2022 Day 13
- Advent of Code 2022 Day 14
- Advent of Code 2022 Day 15
- Advent of Code 2022 Day 16
- Advent of Code 2022 Day 17
- Advent of Code 2022 Day 18
- Advent of Code 2022 Day 19
- Advent of Code 2022 Day 20
- Advent of Code 2022 Day 21
- Advent of Code 2022 Day 22
- Advent of Code 2022 Day 23
- Advent of Code 2022 Day 24
- Advent of Code 2022 Day 25
-
Older post
-
Newer post
Advent of Code 2022 Day 22
Day 22: Monkey Map
https://adventofcode.com/2022/day/22
The monkeys take you through the jungle on the way to the grove.
You have to enter a password to get it.
That password can be found be following a specific path through a maze.
The first half of today’s input is the maze. Where ”.” is an open space, and ”#” is a solid wall. The maze is strangely shaped, so there are a bunch of spaces in the input.
The second part of the input is a list of instructions.
- Numbers mean “move X steps forward”.
- “L” means turn 90 degrees left
- “R” means turn 90 degrees right
An example input looks like this:
- You start the maze at first open tile on the top row.
- You start facing to the right
So the first instruction tells you to take 10 steps on the map in the right direction.
- If an instructions tells you to walk into a wall, stop instead.
- If an instruction sends you off an edge, appear at the opposite one.
The password can be constructed using the final row, column, and direction you are facing.
- Rows start from 1 at the top and count downward
- Columns start from 1 at the left and count rightward.
- Facing scores are:
- 0 for right
- 1 for down
- 2 for left
- 3 for up
The password is 1000 * row + 4 * column + facing.
Parsing
An instruction is an enum that’s either a rotation or a movement.
I parsed the maze as a list of lists. Where items in the outer list are rows. Items in the rows are a tile or an empty space (because of the weird shape of the maze).
The tiles in the map are either open or solid. Because the maze has such a weird shape, I included the spaces where there isn’t anything too. You could also do this by associating a coordinate to each tile in the maze.
Part 1
The question asks what the final password is.
If an instruction sends you off the right side of the map, appear left. Same rule for left to right, up to down, and down to up.
It’s possible there is a wall just off the edge of the maze. And you should stop when you encounter a wall.
The question text has a bunch of visual examples that are useful.
Helpers
You better believe I pulled out the Coord
struct again to represent a position.
The direction you’re facing also gets a data type. Along with a few methods on it.
score
helps with the scoring logic described aboveturn
takes in a direction, and applies a left or right turnoffset
returns what a step in the current direction would do to the current coordinates on the map.
In a pseudocode/skeletoncode hybrid, this is what I wrote:
A helper function that handles the wrapping logic:
Final code
Part 2
The maze isn’t flat, it’s a very large cube. Each side is 50 tiles long.
So, that’s what that weird shape was about. The input “folds” into a cube shape.
The wrapping rules are different now. You continue along the cube instead.
Again, the question text has a few useful examples.
2 example movements around the cube:
- you are at A and move to the right, you arrive at B facing down
- you are at C and move down, you arrive at D facing up:
The question asks what the final password is.
The code for part2 is very similar again. The part in the logic where we apply the wrapping logic is now different.
A step to a different cube face also changes the direction you are facing! So if we take a step, we not only update the current coordinate, but also the facing direction.
Helpers
The helper that handles the wrapping logic got way more complicated.