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 5
Day 5: Supply Stacks
https://adventofcode.com/2022/day/5
Today, the elves have to reoganize the crates in the ship’s cargo hold.
Each crate has a letter to identify it.
Your input has the starting positions of the stacks, and a list of move instructions.
An example input looks like this:
In this example there are 3 platforms.
- The stack on platform 1, from bottom to the top has crates:
Z
, andN
. - The stack on platform 2, from bottom to the top has crates:
M
,C
, andD
. - The stack on platform 3, from bottom to the top has crates:
P
.
In each move instruction, an amount of crates is moved from one platform to another.
Implementation
I started by parsing that input into useful data structures.
This is how I want to represent that input in Rust:
- Each crate is identified by a letter: a
char
. - Each platform has a stack of crates: a
Vec<char>
. - There are multiple stacks: a
Vec<Vec<char>>
.
Desired result: the starting positions of the stacks in the input is parsed as a Vec<Vec<char>>
.
Each instruction has 3 parts:
- an amount of crates to be moved
- the number of a platform’s stack to take the crates from
- the number of a platform’s stack to move to crates to
So I created a struct
with those fields.
Desired result: the list of instructions in the input is parsed as a Vec<Instruction>
.
Parsing the input
Split the entire input file on two newlines. The first part is the starting positions, and the second part is the move instructions.
I took the last line off the starting positions, that’s the line with the platform numbers.
I figure out how many stacks there are by finding the last platform number and create that many empty vectors.
Starting positions
I loop through the lines of the starting positions in reverse. Whenever I find a crate (a letter) I push it into the corresponding stack.
Each crate is represented by [X]
, where X
is a different letter.
Each stack is seperated by a space.
If there is no crate in a line, the space where one would be has three spaces instead of the [X]
.
So for every line in that loop, I look at chunks of 4 (3 for the potential crate + 1 in between).
If the second item in that chunk is a letter, it’s the letter for a crate.
I push it into stack with that chunk’s index.
Move instructions
Parsing the list of instructions into a Vec<Instruction>
is more straightforward.
Each instruction has the exact same structure.
I split on some words that appear in each one, and convert that line to an Instruction
.
Final code
Part 1
The move instructions are performed by a crane that can move one crate at a time.
If the instruction says to move 3 crates from stack 1 to stack 3, it does 3 seperate moves of a single crate.
The question asks to find which crates end up on top of each stack after all instructions are done.
To do that, I loop through the instructions to perform them.
For each instruction I loop amount
times.
pop()
ing a crate off the top of the from
stack, and push()
ing that crate to the top of the to
stack.
At the end, I take the top crate in each stack, and add those char
s together into a String
.
Part 2
The crane turns out to be a newer model that move multiple crates at once.
The question asks to find which crates end up on top of each stack after all instructions are done again.
To do that, I loop through the instructions to perform them.
For each instruction I remove amount
crates from the from
stack, and add them to the to
stack.
Rust has a handy method to do that called split_off
.
At the end, I take the top crate in each stack, and add those char
s together into a String
.