One of The Historians needs to use the bathroom; fortunately, you know there's a bathroom near an unvisited location on their list, and so you're all quickly teleported directly to the lobby of Easter Bunny Headquarters.
Unfortunately, EBHQ seems to have "improved" bathroom security again after your last visit. The area outside the bathroom is swarming with robots!
To get The Historian safely to the bathroom, you'll need a way to predict where the robots will be in the future. Fortunately, they all seem to be moving on the tile floor in predictable straight lines.
You make a list (your puzzle input) of all of the robots' current positions (p
) and velocities (v
), one robot per line. For example:
123456789101112p=0,4 v=3,-3
p=6,3 v=-1,-3
p=10,3 v=-1,2
p=2,0 v=2,-1
p=0,0 v=1,3
p=3,0 v=-2,-2
p=7,6 v=-1,-3
p=3,0 v=-1,-2
p=9,3 v=2,3
p=7,3 v=-1,2
p=2,4 v=2,-3
p=9,5 v=-3,-3
Each robot's position is given as p=x,y
where x
represents the number of tiles the robot is from the left wall and y
represents the number of tiles from the top wall (when viewed from above). So, a position of p=0,0
means the robot is all the way in the top-left corner.
Each robot's velocity is given as v=x,y
where x
and y
are given in tiles per second. Positive x
means the robot is moving to the right, and positive y
means the robot is moving down. So, a velocity of v=1,-2
means that each second, the robot moves 1
tile to the right and 2
tiles up.
The robots outside the actual bathroom are in a space which is 101
tiles wide and 103
tiles tall (when viewed from above). However, in this example, the robots are in a space which is only 11
tiles wide and 7
tiles tall.
The robots are good at navigating over/under each other (due to a combination of springs, extendable legs, and quadcopters), so they can share the same tile and don't interact with each other. Visually, the number of robots on each tile in this example looks like this:
12345671.12.......
...........
...........
......11.11
1.1........
.........1.
.......1...
These robots have a unique feature for maximum bathroom security: they can teleport. When a robot would run into an edge of the space they're in, they instead teleport to the other side, effectively wrapping around the edges. Here is what robot p=2,4 v=2,-3
does for the first few seconds:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253Initial state:
...........
...........
...........
...........
..1........
...........
...........
After 1 second:
...........
....1......
...........
...........
...........
...........
...........
After 2 seconds:
...........
...........
...........
...........
...........
......1....
...........
After 3 seconds:
...........
...........
........1..
...........
...........
...........
...........
After 4 seconds:
...........
...........
...........
...........
...........
...........
..........1
After 5 seconds:
...........
...........
...........
.1.........
...........
...........
...........
The Historian can't wait much longer, so you don't have to simulate the robots for very long. Where will the robots be after 100
seconds?
In the above example, the number of robots on each tile after 100 seconds has elapsed looks like this:
1234567......2..1.
...........
1..........
.11........
.....1.....
...12......
.1....1....
To determine the safest area, count the number of robots in each quadrant after 100 seconds. Robots that are exactly in the middle (horizontally or vertically) don't count as being in any quadrant, so the only relevant robots are:
1234567..... 2..1.
..... .....
1.... .....
..... .....
...12 .....
.1... 1....
In this example, the quadrants contain 1
, 3
, 4
, and 1
robot. Multiplying these together gives a total safety factor of 12
.
Predict the motion of the robots in your list within a space which is 101
tiles wide and 103
tiles tall. What will the safety factor be after exactly 100 seconds have elapsed?
224246880
I used zod
to transform the input into structured data:
123456789101112131415161718192021222324const parser = z.array(
z
.string()
.transform((value) => {
const groups = value.replace("p=", "").replace(" v=", ",").split(",");
return {
px: groups[0],
py: groups[1],
vx: groups[2],
vy: groups[3],
};
})
.pipe(
z.object({
px: z.coerce.number(),
py: z.coerce.number(),
vx: z.coerce.number(),
vy: z.coerce.number(),
})
)
);
const values = parser.parse(input.split("\n"));
The robots keep track of their position and velocity, and are randomly assigned a color on instantiation. Each robot has a step
method and a quadrant
method, which calculate the current quadrant the robot is in (if it's in one).
The only unusual part is the step
method, which has some extra logic to "teleport" the robot to the other side of the map if it leaves the map.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455type Quadrant = "q1" | "q2" | "q3" | "q4";
type RobotProps = z.infer<typeof parser>[number];
class Robot {
px: number;
py: number;
vx: number;
vy: number;
constructor(props: RobotProps) {
this.px = props.px;
this.py = props.py;
this.vx = props.vx;
this.vy = props.vy;
}
step() {
this.px += this.vx;
this.py += this.vy;
while (this.px < 0) {
this.px = WIDTH + this.px;
}
while (this.px > WIDTH) {
this.px = this.px - WIDTH;
}
while (this.py < 0) {
this.py = HEIGHT + this.py;
}
while (this.py > HEIGHT) {
this.py = this.py - HEIGHT;
}
}
quadrant(): Quadrant | null {
const left = this.px >= 0 && this.px < Math.floor(WIDTH / 2);
const right = this.px >= Math.ceil(WIDTH / 2) && this.px <= WIDTH;
const top = this.py >= 0 && this.py < Math.floor(HEIGHT / 2);
const bottom = this.py >= Math.ceil(HEIGHT / 2) && this.py <= HEIGHT;
if (left && top) return "q1";
if (right && top) return "q2";
if (right && bottom) return "q3";
if (left && bottom) return "q4";
return null;
}
}
The map keeps track of robots and provides some helper methods.
1234567891011121314151617181920212223242526272829class Map {
robots: Array<Robot>;
constructor() {
this.robots = values.map((value) => new Robot(value));
}
step() {
this.robots.forEach((robot) => robot.step());
}
safetyFactor() {
const quadrantCount: Record<Quadrant, number> = {
q1: 0,
q2: 0,
q3: 0,
q4: 0,
};
this.robots.forEach((robot) => {
const quadrant = robot.quadrant();
if (quadrant) quadrantCount[quadrant] += 1;
});
return quadrantCount.q1 * quadrantCount.q2 * quadrantCount.q3 * quadrantCount.q4;
}
}
During the bathroom break, someone notices that these robots seem awfully similar to ones built and used at the North Pole. If they're the same type of robots, they should have a hard-coded Easter egg: very rarely, most of the robots should arrange themselves into a picture of a Christmas tree.
What is the fewest number of seconds that must elapse for the robots to display the Easter egg?
6888
, so it'll take that many iterations until the result is displayed. I've hard-coded the animation timing to look good for my input.
To find the Easter egg, we run the robots until the robots are in a unique (non-overlapping) formation.
This uses almost identical code to Part 1, but adds a new method to the Map
class:
1234567891011121314151617181920class Map {
...
robotPositionsAreUnique() {
const seenPositions = new Set<string>();
for (const robot of this.robots) {
const strPosition = `${robot.py},${robot.px}`;
if (seenPositions.has(strPosition)) return false;
seenPositions.add(strPosition);
}
return true;
}
...
}
Part 1 Time | Part 1 Rank | Part 2 Time | Part 2 Rank |
---|---|---|---|
00:44:08 | 4,955 | 01:04:10 | 3,008 |
Part 1 was really fun! The "teleport" logic took a little bit of thought, but the problem was very straightforward and very fun to animate.
I think Part 2 was more than a little ridiculous. I had no idea what I was even trying to solve for, so I ended up looking at the Advent of Code subreddit to see what others were even trying to do. If the prompt had provided any sort of guidance (i.e. "robots must not occupy the same space"), this would've been both solvable and fun! As is, the prompt left me totally stuck with no ideas what to even try doing.
When I saw Part 1, I thought this would've been a (nearly) identical problem to Advent of Code 2023 Day 14, where Part 2 required you to find positions after far more steps that what Part 1 asked for. I was really excited about this, as I enjoyed this challenge last year, even though Part 2 took over an hour to solve on it's own.
Part 2 for this year was a bit disappointing. But I'm also glad it was easy to code once I understood the challenge.