From d8839505913d98ce70875396e2e5186de3eaf128 Mon Sep 17 00:00:00 2001 From: Peery Date: Mon, 5 Dec 2022 16:24:52 +0100 Subject: [PATCH] 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. --- day5/src/main.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/day5/src/main.rs b/day5/src/main.rs index c18499c..8ca9b51 100644 --- a/day5/src/main.rs +++ b/day5/src/main.rs @@ -98,10 +98,17 @@ fn simulate_stack_operation(mut stacks: Vec>, 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 = 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;