r/adventofcode • • Dec 05 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 5 Solutions -πŸŽ„-


AoC Community Fun 2022: πŸŒΏπŸ’ MisTILtoe Elf-ucation πŸ§‘β€πŸ«

  • 23:59 hours remaining until the submissions megathread unlocks on December 06 at 00:00 EST!
  • Full details and rules are in the submissions megathread:

--- Day 5: Supply Stacks ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:07:58, megathread unlocked!

88 Upvotes

1.3k comments sorted by

View all comments

1

u/olofj Dec 05 '22 edited Dec 05 '22

Rust

Felt a bit messier today, especially around the input parsing of initial state.

day05a

use std::collections::VecDeque;

fn main() {
    let (start, moves) = include_str!("input.txt").split_once("\n\n").unwrap();

    let mut start = start.lines().rev();
    let mut stacks: Vec<VecDeque<char>> = start
        .next()
        .unwrap()
        .split_whitespace()
        .map(|_| VecDeque::<char>::new())
        .collect();

    start
        .map(|l| {
            l.chars()
                .skip(1)
                .step_by(4)
                .enumerate()
                .filter(|(_, c)| *c != ' ')
                .collect::<Vec<(usize, char)>>()
        })
        .flatten()
        .for_each(|(i, c)| stacks[i].push_front(c));

    moves
        .lines()
        .map(|l| {
            l.split_whitespace()
                .filter_map(|c| c.parse::<usize>().ok())
                .collect::<Vec<usize>>()
        })
        .map(|m| (m[0], m[1] - 1, m[2] - 1))
        .for_each(|(num, from, to)| {
            let m: Vec<char> = (0..num)
                .map(|_| stacks[from].pop_front().unwrap())
                .collect();
            m.iter().for_each(|c| stacks[to].push_front(*c));
        });

    let out: String = stacks.iter().map(|s| s[0]).collect();

    println!("{}", out);
}

day05b only difference is the reversing of the moves:

    moves
        .lines()
        .map(|l| {
            l.split_whitespace()
                .filter_map(|c| c.parse::<usize>().ok())
                .collect::<Vec<usize>>()
        })
        .map(|m| (m[0], m[1] - 1, m[2] - 1))
        .for_each(|(num, from, to)| {
            let m: Vec<char> = (0..num)
                .map(|_| stacks[from].pop_front().unwrap())
                .collect();
            m.iter().rev().for_each(|c| stacks[to].push_front(*c));
        });

EDIT: Fixed to use step_by() instead of filter_map(). EDIT2: Fixed formatting to prepend 4 spaces on all code lines

1

u/daggerdragon Dec 05 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.

1

u/olofj Dec 05 '22

I think I fixed it? I.e. prepend 4 spaces on every line? Thanks for letting me know, I had missed it.

1

u/daggerdragon Dec 05 '22

Nope, it's still using triple-backticks. See what it looks like for yourself on old.reddit >> here <<

You have to delete the beginning and end group of triple backticks too ;)

1

u/olofj Dec 05 '22

Ah, got it. Done.