Day 5 - Supply Stacks (puzzle 2)

Just changed the instruction execution to run with a temporary crane stack.
First all crates are fetched from the source stack and put onto the crane stack.
Afterwards all crates on the crane stack are put onto the target stack.

Both operations use stacks and therefore result in a same-order movement in the end.
main
Peery 2 years ago
parent 8783562d39
commit d883950591

@ -98,10 +98,17 @@ fn simulate_stack_operation(mut stacks: Vec<VecDeque<char>>, instruction: [u32;
let target: u32 = instruction[2]-1;
//println!("Executing instruction {:?}", instruction);
for _ in 0..amount { // do the instruction amount times
let mut crane_stack: Vec<char> = Vec::new();
for _ in 0..amount { // fetch amount-many crates onto the crane stack
let c: char = stacks[source as usize].pop_back().unwrap();
//println!("Took crate {} from stack #{} and placed it onto stack #{}", c, source, target);
crane_stack.push(c);
println!("Took crate {} from stack #{} and placed it onto the crane stack {:?}", c, source, crane_stack);
}
for _ in 0..amount { // drop all crates in the crane stack onto the target stack
let c: char = crane_stack.pop().unwrap();
stacks[target as usize].push_back(c);
println!("Placed crate {} from the crane stack onto target stack #{}", c, target);
}
return stacks;

Loading…
Cancel
Save