Advent of Code 2022 Day 5 Solution: Ordering Crates

Day 5 of the festive coding challenge is all about crate stacking, can you solve it?

Ashley Peacock

--

Photo by Hemerson Coelho on Unsplash

I’m back for another day of Advent of Code fun, and day 5 is all about arrays and moving crates around!

If you’ve been following along with me, I’m sure you know what Advent of Code is by now, so let’s get cracking with day 5!

Day 5 Problem: Supply Stacks

The problem today is my favourite so far, I really enjoyed it, as it had lots of little quirks in it. In short, we are given a starting arrangement for a set of crates. Crates are stacked one on top of the other, and each have an identifier (a letter).

We are also given a set of instructions of how to move the crates, so we must work out the end state of the crates. To complete the first challenge for day 5, we must give the top crate from each stack as the end result.

Here’s an example starting position, and a few commands:

    [D]    
[N] [C]
[Z] [M] [P]
1 2 3

move 1 from 2 to 1
move 3 from 1 to 3

In this example, there are three stacks. The first has two crates (N & Z), the second stack has three crates (D, C & M) and the third has one crate (P).

--

--