pub fn y22d05(input: &str, part: u32) -> String
Expand description
The solution for the day five challenge.
As the arguments correspond to the input text and the method of operation:
the reordering of crates (part 1
) or the maintaining of the order (part
2
).
§Parsing the Initial State
To parse the initial state we first need to get all of the lines before the first empty line which we can treat as the delimiter between the state representation and the moves that we will need to make. The last non-empty line of the state representation gives us a numbering of the columns (which we can use to determine the total number of columns) that needs to be discarded as it is not actually a part of the internal state representation. Once we have the lines that represent the state we can loop through them (in reverse order so that the items on the bottom of the stacks are the first items in the vector) checking each column to see if there is an element defined there or not (e.g, empty space or end-of-input) and if there is then add it to the appropriate column vector.
§Example
// probably read this in from the input file...
let input = concat![
" [D] \n",
"[N] [C] \n",
"[Z] [M] [P]\n",
" 1 2 3 \n",
"\n",
"move 1 from 2 to 1\n",
"move 3 from 1 to 3\n",
"move 2 from 2 to 1\n",
"move 1 from 1 to 2\n",
];
assert_eq!(y22d05(&input, 1), "CMZ");
assert_eq!(y22d05(&input, 2), "MCD");