Function aoc::year2022::day05::parse

source ยท
pub fn parse(input: &str) -> (Vec<Vec<char>>, Vec<[usize; 3]>)
Expand description

Parses the input in 2 stages.

First, the input is split into a prefix and suffix, using a blank line (or 2 newline characters one after another) as the delimiter.

The suffix consisting of triplets of (amount, from, to) can be parsed using our utility iter_unsigned and chunk methods to tokenize the string into numbers, then group it into triples. One minor nuance is that the from and to field are 1 based indexing, so we convert them to 0 based for convenience.

The prefix is a little more complex. The number of columns is the width in characters plus 1 divided by 4 (the last column has no trailing space). Then we build the vectors from the bottom up by iterating through the rows in reverse. This places the elements at the top of each stack at the end of the vec which is a more natural location for mutation (as removing elements from the start of a vec involved moving all remaining elements).