use day5::*;

use aoc::{answer, parser};

fn main() {
    let input = parser::input::<String>();
    let mut input = input.split("\n\n");

    let setup = input.next().unwrap();
    let mut setup: Crates = setup.parse().unwrap();

    let instructions = input.next().unwrap();
    let instructions = instructions
        .lines()
        .map(|s| s.parse::<Instruction>().unwrap())
        .collect::<Vec<_>>();

    for instr in instructions {
        println!("{}", setup);
        println!(" {} ", "-".repeat(setup.crates.len() * 5 - 2));
        println!();

        let len = setup.crates[instr.from - 1].len();

        let mut crates = setup.crates[instr.from - 1]
            .drain(len - instr.number..)
            .collect::<Vec<char>>();

        setup.crates[instr.to - 1].append(&mut crates);
    }

    let answer = setup
        .crates
        .iter()
        .filter_map(|stack| stack.last())
        .collect::<String>();

    answer!(
        "After the rearrangement procedure completes the crates that ends up on top of each stack are {}.",
        answer
    );
}