From cf69f9f93dbafcf1c255c97488c4b8ba49d37f9c Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Sun, 4 Dec 2022 23:48:47 -0600 Subject: [PATCH] Part 2 --- 2022/rust/src/day5.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/2022/rust/src/day5.rs b/2022/rust/src/day5.rs index 1eeac3b..1eac46f 100644 --- a/2022/rust/src/day5.rs +++ b/2022/rust/src/day5.rs @@ -81,15 +81,36 @@ fn part1(input: &mut Input) -> Result { r.trim().to_string() } -fn part2(input: &Input) -> Result { - "".to_string() +fn perform_moves2(crates: &mut Crates) { + let stacks = &mut crates.stacks; + for Move { amount, from, to } in &crates.moves { + println!("move: {} {} {}", amount, from, to); + let l = stacks[*from].len(); + println!("len: {:?}", l); + let ra = l - (*amount as usize)..l; + let mut popped: Vec = stacks[*from].splice(ra, vec![]).collect(); + stacks[*to].append(&mut popped); + println!("{:?}", stacks); + } +} + +fn part2(input: &mut Input) -> Result { + println!("{:?}", input); + perform_moves2(input); + + let mut r = String::from(""); + for s in &input.stacks { + r.push(s.last().unwrap_or(&' ').clone()) + } + r.trim().to_string() } fn main() { let input_text = common::day_input(5); eprintln!("{}\n\nAbove is your input file.\n\n", input_text); let mut input = processed_input(&input_text); - common::show_answers(&part1(&mut input), &part2(&input)) + let mut input2 = processed_input(&input_text); + common::show_answers(&part1(&mut input), &part2(&mut input2)) // common::show_both_answers(&both_parts(&input)) } @@ -116,7 +137,7 @@ move 1 from 1 to 2"; let mut input = processed_input(TEST_INPUT); assert_eq!(part1(&mut input), "CMZ"); let mut input = processed_input(TEST_INPUT); - assert_eq!(part2(&input), ""); + assert_eq!(part2(&mut input), "MCD"); // assert_eq!(both_parts(&input), (0, 0)); } }