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 9
Day 9: Rope Bridge
https://adventofcode.com/2022/day/9
The expedition has to cross a rickety rope bridge.
You decide to distract yourself by thinking about food rope physics.
Consider a rope with knots at each end.
- A knot at the start, the head.
- A knot at the end, the tail.
When the head moves far enough away, the tail has to follow.
The question does some magical handwaving, and now knots can be represented on a 2D grid of integers.
Today’s input is a list of motions the head takes.
An example input looks like this:
This series of motions moves the head right four steps, then up four steps, then left three steps, then down one step, and so on.
Part 1
It’s a short rope, consisting only of the head and the tail, with no space in between.
The head and the tail must always be touching.
Touching means one of 2 thing is true:
- Adjacent in one of the 8 directions (horizontal, vertical, or diagonal)
- Completely overlapping.
If the head is ever 2 steps away from the tail (meaning: they are no longer touching), the tail must catch up.
- If the head is ever two steps directly up, down, left, or right from the tail, the tail must also move one step in that direction so it remains close enough.
- If the head and tail aren’t touching and aren’t in the same row or column, the tail always moves one step diagonally to keep up
The head and the tail both start at the same position, at the coordinates (0,0).
The question asks how many positions the tail of the rope visited at least once?
The positions the tail visited can be tracked in a set. For every line in the input, we simulate the rope, and insert the new position of the tail into that set.
In pseudocode, that would be:
I decided to make a Coord
struct to represent a coordinate.
To start writing out the solution:
The part where we move the head is straightforward, it’s the moving of the tail that’s the tricky part.
To do that, we first determine if the tail is touching the head. We do that by calculating the difference between the head and the tail first.
The knots aren’t touching if the distance in any direction is larger than 1.
The way the tail catches up is described by the rules above.
It turns out that catching up is equal to adding the sign of the difference in each direction!
The updated code is the answer to part1!
Final code
Part 2
You now have to simulate a rope that has 10 knots instead of 2.
The question asks how many positions the tail of the rope visited at least once?
As in previous days, a lot of the part1 code is reused.
Instead of 2 variables for head
and tail
, we now track 10 variables.
One for each knot in the rope.
In the implementation, each knot is stored as a coordinate in a list called rope
.
The rope follows the same rules as in part1, so that logic remains unchanged.
For each step:
We first move the head of the entire rope.
Then, we iterate over 2 sections of the rope at a time.
- The first is the head of that section and never moves.
- The second is the tail of that section and catches up if necessary.
If the tail of a section is also the tail of the entire rope, we insert it into seen
if it moved.