Advent of Code 2022 - Rust Edition - Day 5

Day 5: Supply Stacks

--- Part One ---

The expedition can depart as soon as the final supplies have been unloaded from the ships. Supplies are stored in stacks of marked crates, but because the needed supplies are buried under many other crates, the crates need to be rearranged.

The ship has a giant cargo crane capable of moving crates between stacks. To ensure none of the crates get crushed or fall over, the crane operator will rearrange them in a series of carefully-planned steps. After the crates are rearranged, the desired crates will be at the top of each stack.

The Elves don't want to interrupt the crane operator during this delicate procedure, but they forgot to ask her which crate will end up where, and they want to be ready to unload them as soon as possible so they can embark.

They do, however, have a drawing of the starting stacks of crates and the rearrangement procedure (your puzzle input). For example:

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

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

In this example, there are three stacks of crates. Stack 1 contains two crates: crate Z is on the bottom, and crate N is on top. Stack 2 contains three crates; from bottom to top, they are crates M, C, and D. Finally, stack 3 contains a single crate, P.

Then, the rearrangement procedure is given. In each step of the procedure, a quantity of crates is moved from one stack to a different stack. In the first step of the above rearrangement procedure, one crate is moved from stack 2 to stack 1, resulting in this configuration:

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

In the second step, three crates are moved from stack 1 to stack 3. Crates are moved one at a time, so the first crate to be moved (D) ends up below the second and third crates:

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

Then, both crates are moved from stack 2 to stack 1. Again, because crates are moved one at a time, crate C ends up below crate M:

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

Finally, one crate is moved from stack 1 to stack 2:

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

The Elves just need to know which crate will end up on top of each stack; in this example, the top crates are C in stack 1, M in stack 2, and Z in stack 3, so you should combine these and give the Elves the message CMZ.

After the rearrangement procedure completes, what crate ends up on top of each stack?

--- Part Two ---

As you watch the crane operator expertly rearrange the crates, you notice the process isn't following your prediction.

Some mud was covering the writing on the side of the crane, and you quickly wipe it away. The crane isn't a CrateMover 9000 - it's a CrateMover 9001.

The CrateMover 9001 is notable for many new and exciting features: air conditioning, leather seats, an extra cup holder, and the ability to pick up and move multiple crates at once.

Again considering the example above, the crates begin in the same configuration:

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

Moving a single crate from stack 2 to stack 1 behaves the same as before:

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

However, the action of moving three crates from stack 1 to stack 3 means that those three moved crates stay in the same order, resulting in this new configuration:

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

Next, as both crates are moved from stack 2 to stack 1, they retain their order as well:

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

Finally, a single crate is still moved from stack 1 to stack 2, but now it's crate C that gets moved:

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

In this example, the CrateMover 9001 has put the crates in a different order: MCD.

Before the rearrangement process finishes, update your simulation so that the Elves know where they should stand to be ready to unload the final supplies. After the rearrangement procedure completes, what crate ends up on top of each stack?

--- Solution ---

pub struct Instruction {
    number_of_containers: usize,
    from: usize,
    to: usize,
}

type Crate = char;
type Stack = Vec<Crate>;

pub fn parse(input: &'static str) -> (Vec<Stack>, Vec<Instruction>) {
    let (schema, instructions) = input.split_once("\n\n").unwrap();

    let graph: Vec<_> = schema.lines().collect();

    let graph_len = graph[0].len() / 4 + 1;
    let mut stacks: Vec<Stack> = vec![Vec::with_capacity(graph.len() - 1); graph_len + 1];

    for row_number in (0..graph.len() - 1).rev() {
        let row = graph[row_number];
        for (stack_number, stack) in stacks.iter_mut().enumerate().skip(1).take(graph_len) {
            let container = row.chars().nth((stack_number - 1) * 4 + 1).unwrap();

            if !container.is_ascii_whitespace() {
                stack.push(container);
            }
        }
    }

    let instructions = instructions
        .lines()
        .map(|instruction| {
            let mut numbers = instruction
                .split_ascii_whitespace()
                .filter_map(|token| token.parse().ok());

            Instruction {
                number_of_containers: numbers.next().unwrap(),
                from: numbers.next().unwrap(),
                to: numbers.next().unwrap(),
            }
        })
        .collect();

    (stacks, instructions)
}

pub fn solve_part_one((stacks, instructions): (Vec<Stack>, Vec<Instruction>)) -> String {
    let mut stacks = stacks.to_owned();

    for instruction in instructions {
        for _ in 0..instruction.number_of_containers {
            let supply_crate = stacks[instruction.from].pop().unwrap();
            stacks[instruction.to].push(supply_crate);
        }
    }

    stacks
        .iter()
        .filter_map(|stack| stack.iter().last())
        .collect()
}

pub fn solve_part_two((stacks, instructions): (Vec<Stack>, Vec<Instruction>)) -> String {
    let mut stacks = stacks;

    for instruction in instructions {
        let from = &mut stacks[instruction.from];
        let mut moved = from.split_off(from.len() - instruction.number_of_containers);

        stacks[instruction.to].append(&mut moved);
    }

    stacks
        .iter()
        .filter_map(|stack| stack.iter().last())
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn part_one() {
        let parsed = parse(include_str!("./test_input.txt"));
        let result = solve_part_one(parsed);
        assert_eq!(result, "CMZ".to_string())
    }

    #[test]
    fn part_two() {
        let parsed = parse(include_str!("./test_input.txt"));
        let result = solve_part_two(parsed);
        assert_eq!(result, "MCD".to_string())
    }
}