It's time again for the Reindeer Olympics! This year, the big event is the Reindeer Maze, where the Reindeer compete for the lowest score.
You and The Historians arrive to search for the Chief right as the event is about to start. It wouldn't hurt to watch a little, right?
The Reindeer start on the Start Tile (marked S
) facing East and need to reach the End Tile (marked E
). They can move forward one tile at a time (increasing their score by 1
point), but never into a wall (#
). They can also rotate clockwise or counterclockwise 90 degrees at a time (increasing their score by 1000
points).
To figure out the best place to sit, you start by grabbing a map (your puzzle input) from a nearby kiosk. For example:
123456789101112131415###############
#.......#....E#
#.#.###.#.###.#
#.....#.#...#.#
#.###.#####.#.#
#.#.#.......#.#
#.#.#####.###.#
#...........#.#
###.#.#####.#.#
#...#.....#.#.#
#.#.#.###.#.#.#
#.....#...#.#.#
#.###.#.#.#.#.#
#S..#.....#...#
###############
There are many paths through this maze, but taking any of the best paths would incur a score of only 7036
. This can be achieved by taking a total of 36
steps forward and turning 90 degrees a total of 7
times:
123456789101112131415###############
#.......#....E#
#.#.###.#.###^#
#.....#.#...#^#
#.###.#####.#^#
#.#.#.......#^#
#.#.#####.###^#
#..>>>>>>>>v#^#
###^#.#####v#^#
#>>^#.....#v#^#
#^#.#.###.#v#^#
#^....#...#v#^#
#^###.#.#.#v#^#
#S..#.....#>>^#
###############
Here's a second example:
1234567891011121314151617#################
#...#...#...#..E#
#.#.#.#.#.#.#.#.#
#.#.#.#...#...#.#
#.#.#.#.###.#.#.#
#...#.#.#.....#.#
#.#.#.#.#.#####.#
#.#...#.#.#.....#
#.#.#####.#.###.#
#.#.#.......#...#
#.#.###.#####.###
#.#.#...#.....#.#
#.#.#.#####.###.#
#.#.#.........#.#
#.#.#.#########.#
#S#.............#
#################
In this maze, the best paths cost 11048
points; following one such path would look like this:
1234567891011121314151617#################
#...#...#...#..E#
#.#.#.#.#.#.#.#^#
#.#.#.#...#...#^#
#.#.#.#.###.#.#^#
#>>v#.#.#.....#^#
#^#v#.#.#.#####^#
#^#v..#.#.#>>>>^#
#^#v#####.#^###.#
#^#v#..>>>>^#...#
#^#v###^#####.###
#^#v#>>^#.....#.#
#^#v#^#####.###.#
#^#v#^........#.#
#^#v#^#########.#
#S#>>^..........#
#################
Note that the path shown above includes one 90 degree turn as the very first move, rotating the Reindeer from facing East to facing North.
Analyze your map carefully. What is the lowest score a Reindeer could possibly get?
The code is fairly messy, but does complete in a reaonably-quick time (~1s).
Previous positions are kept track of as we iterate through the maze. This consumes quite a bit of memory, but makes retrieval of the path trivial.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274interface Position {
y: number;
x: number;
}
type Direction = "up" | "down" | "left" | "right";
type StrPosition = `${string},${string}`;
interface NextPosition extends Position {
prevScore: number;
direction: Direction;
prevPositions: Set<StrPosition>;
}
interface ScoredPositions {
score: number;
positions: Array<Position>;
}
class Map {
start: Position;
end: Position;
matrix: Array<Array<string>>;
scoredMatrix: Array<Array<Record<Direction, ScoredPositions>>>;
finalScore: number | null;
searching: boolean;
bestPositions: Set<StrPosition>;
positionsToCheck: Array<NextPosition>;
constructor() {
this.matrix = JSON.parse(JSON.stringify(values));
this.scoredMatrix = values.map((row) => {
return row.map(() => {
return {
up: { score: Infinity, positions: [] },
down: { score: Infinity, positions: [] },
left: { score: Infinity, positions: [] },
right: { score: Infinity, positions: [] },
};
});
});
this.start = { y: -1, x: -1 };
this.end = { y: -1, x: -1 };
this.matrix.forEach((row, y) => {
row.forEach((character, x) => {
if (character === "S") {
this.start = { y, x };
}
if (character === "S") {
this.end = { y, x };
}
});
});
this.finalScore = null;
this.searching = true;
this.bestPositions = new Set();
// Start with the start position
this.positionsToCheck = [
{
// Up
y: this.start.y - 1,
x: this.start.x,
prevScore: 1001,
direction: "up",
prevPositions: new Set(),
},
{
// Down
y: this.start.y + 1,
x: this.start.x,
prevScore: 1001,
direction: "down",
prevPositions: new Set(),
},
{
// Right
y: this.start.y,
x: this.start.x + 1,
prevScore: 1,
direction: "right",
prevPositions: new Set(),
},
];
}
at({ y, x }: Position) {
const tile = this.matrix[y]?.[x];
if (tile === undefined) {
throw new Error(`Bad position: ${y},${x}`);
}
return tile;
}
addPositionToCheck(position: NextPosition) {
// Skip walls
if (this.at(position) === "#") return;
const { y, x, direction, prevScore } = position;
// Add score
const score = this.scoredMatrix[y]![x]![direction].score;
if (prevScore < score) {
// Prune paths
this.scoredMatrix[y]![x]![direction].score = prevScore;
this.scoredMatrix[y]![x]![direction].positions = [];
this.scoredMatrix[y]![x]![direction].positions.push({ y, x });
this.positionsToCheck.push(position);
}
}
step() {
// Get a position to check
const position = this.positionsToCheck.shift();
if (!position) {
throw new Error("No positions left to check");
}
const tile = this.at(position);
// Hit a wall, early exit
if (tile === "#") return;
if (tile === "E") {
// Hit the end
this.finalScore = position.prevScore;
position.prevPositions.forEach((prevPosition) => {
this.bestPositions.add(prevPosition);
});
this.searching = false;
return;
}
// this.exploreMatrix[position.y]![position.x] = directionCode[position.direction];
const strPosition: StrPosition = `${position.y},${position.x}`;
// We've got a valid tile to move to
// Push into our positions to check
if (position.direction === "up") {
// Go in the same direction
this.addPositionToCheck({
y: position.y - 1,
x: position.x,
prevScore: position.prevScore + 1,
direction: "up",
prevPositions: new Set([...position.prevPositions]).add(strPosition),
});
// 90 degree turns
this.addPositionToCheck({
y: position.y,
x: position.x - 1,
prevScore: position.prevScore + 1001,
direction: "left",
prevPositions: new Set([...position.prevPositions]).add(strPosition),
});
this.addPositionToCheck({
y: position.y,
x: position.x + 1,
prevScore: position.prevScore + 1001,
direction: "right",
prevPositions: new Set([...position.prevPositions]).add(strPosition),
});
}
if (position.direction === "down") {
// Go in the same direction
this.addPositionToCheck({
y: position.y + 1,
x: position.x,
prevScore: position.prevScore + 1,
direction: "down",
prevPositions: new Set([...position.prevPositions]).add(strPosition),
});
// 90 degree turns
this.addPositionToCheck({
y: position.y,
x: position.x - 1,
prevScore: position.prevScore + 1001,
direction: "left",
prevPositions: new Set([...position.prevPositions]).add(strPosition),
});
this.addPositionToCheck({
y: position.y,
x: position.x + 1,
prevScore: position.prevScore + 1001,
direction: "right",
prevPositions: new Set([...position.prevPositions]).add(strPosition),
});
}
if (position.direction === "left") {
// Go in the same direction
this.addPositionToCheck({
y: position.y,
x: position.x - 1,
prevScore: position.prevScore + 1,
direction: "left",
prevPositions: new Set([...position.prevPositions]).add(strPosition),
});
// 90 degree turns
this.addPositionToCheck({
y: position.y - 1,
x: position.x,
prevScore: position.prevScore + 1001,
direction: "up",
prevPositions: new Set([...position.prevPositions]).add(strPosition),
});
this.addPositionToCheck({
y: position.y + 1,
x: position.x,
prevScore: position.prevScore + 1001,
direction: "down",
prevPositions: new Set([...position.prevPositions]).add(strPosition),
});
}
if (position.direction === "right") {
// Go in the same direction
this.addPositionToCheck({
y: position.y,
x: position.x + 1,
prevScore: position.prevScore + 1,
direction: "right",
prevPositions: new Set([...position.prevPositions]).add(strPosition),
});
// 90 degree turns
this.addPositionToCheck({
y: position.y - 1,
x: position.x,
prevScore: position.prevScore + 1001,
direction: "up",
prevPositions: new Set([...position.prevPositions]).add(strPosition),
});
this.addPositionToCheck({
y: position.y + 1,
x: position.x,
prevScore: position.prevScore + 1001,
direction: "down",
prevPositions: new Set([...position.prevPositions]).add(strPosition),
});
}
// Resort the positions by lowest score
this.positionsToCheck.sort((a, b) => a.prevScore - b.prevScore);
}
get score() {
if (this.bestPositions.size === 0) return 0;
// +2 to include start and end
return this.bestPositions.size + 2;
}
}
Now that you know what the best paths look like, you can figure out the best spot to sit.
Every non-wall tile (S
, .
, or E
) is equipped with places to sit along the edges of the tile. While determining which of these tiles would be the best spot to sit depends on a whole bunch of factors (how comfortable the seats are, how far away the bathrooms are, whether there's a pillar blocking your view, etc.), the most important factor is whether the tile is on one of the best paths through the maze. If you sit somewhere else, you'd miss all the action!
So, you'll need to determine which tiles are part of any best path through the maze, including the S
and E
tiles.
In the first example, there are 45
tiles (marked O
) that are part of at least one of the various best paths through the maze:
123456789101112131415###############
#.......#....O#
#.#.###.#.###O#
#.....#.#...#O#
#.###.#####.#O#
#.#.#.......#O#
#.#.#####.###O#
#..OOOOOOOOO#O#
###O#O#####O#O#
#OOO#O....#O#O#
#O#O#O###.#O#O#
#OOOOO#...#O#O#
#O###.#.#.#O#O#
#O..#.....#OOO#
###############
In the second example, there are 64
tiles that are part of at least one of the best paths:
1234567891011121314151617#################
#...#...#...#..O#
#.#.#.#.#.#.#.#O#
#.#.#.#...#...#O#
#.#.#.#.###.#.#O#
#OOO#.#.#.....#O#
#O#O#.#.#.#####O#
#O#O..#.#.#OOOOO#
#O#O#####.#O###O#
#O#O#..OOOOO#OOO#
#O#O###O#####O###
#O#O#OOO#..OOO#.#
#O#O#O#####O###.#
#O#O#OOOOOOO..#.#
#O#O#O#########.#
#O#OOO..........#
#################
Analyze your map further. How many tiles are part of at least one of the best paths through the maze?
The code is nearly identical to Part 1, with some small (but important) changes:
- We don't prune paths that have the same score as our best-scoring path on that tile
- Once we complete the maze, continue building up paths until we no longer have positions to check in our queue that have a score lower than our best final score
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061class Map {
...
addPositionToCheck(position: NextPosition) {
// Skip walls
if (this.at(position) === "#") return;
const { y, x, direction, prevScore } = position;
// Add score
const score = this.scoredMatrix[y]![x]![direction].score;
if (prevScore < score) {
// Prune paths
this.scoredMatrix[y]![x]![direction].score = prevScore;
this.scoredMatrix[y]![x]![direction].positions = [];
this.scoredMatrix[y]![x]![direction].positions.push({ y, x });
this.positionsToCheck.push(position);
} else if (prevScore === score) {
// Don't prune paths that have the same score
this.scoredMatrix[y]![x]![direction].positions.push({ y, x });
this.positionsToCheck.push(position);
}
}
step() {
// Get a position to check
const position = this.positionsToCheck.shift();
if (!position) {
throw new Error("No positions left to check");
}
const tile = this.at(position);
// Hit a wall, early exit
if (tile === "#") return;
if (this.finalScore && position.prevScore > this.finalScore) {
// Out of relevant positions to check
this.searching = false;
return;
}
if (tile === "E") {
// Hit the end
this.finalScore = position.prevScore;
position.prevPositions.forEach((prevPosition) => {
this.bestPositions.add(prevPosition);
});
return;
}
...
}
}
Part 1 Time | Part 1 Rank | Part 2 Time | Part 2 Rank |
---|---|---|---|
01:00:26 | 3,787 | 23:23:38 | 17,941 |
Part 1 was pretty easy! I reworked the code to be much closer to what I've got in Part 2, but my original solution wasn't very different from the end result.
After spending ~4 consecutive hours on Part 2, I decided to call it for the night. It just wasn't clicking for me. I've used Dijkstra's algorithm before, but I just couldn't figure out how to use it here.
I ended up keeping track of the previous positions of the path inside of the lead node. While this isn't very memory-efficient (I'm storing LOTS of sets in memory), it does make the retracing part very straightforward.