From 5bf430a421f71fb77a1c43f69ab6085ef3a63841 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 9 Dec 2022 10:56:30 +0100 Subject: [PATCH] Solve Part 2 --- src/main.rs | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 104f3bb..26a7f4c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,6 +65,7 @@ fn process_crates(crates_and_numbers: &str) -> Vec> result } +#[allow(dead_code)] fn part1() { let (crates, procedures) = split_into_crates_and_procedures(DATA); @@ -101,6 +102,40 @@ fn part1() println!("Final layout is {}", result.as_str()); } -fn main() { - part1(); +fn part2() +{ + let (crates, procedures) = split_into_crates_and_procedures(DATA); + let mut crate_vec = process_crates(crates); + + let procedure_regex = Regex::new(r"move (\d+) from (\d+) to (\d+)").unwrap(); + + for cap in procedure_regex.captures_iter(procedures) + { + let crates_moved: usize = cap[1].parse().unwrap(); + let starting_stack_index: usize = cap[2].parse().unwrap(); + let ending_stack_index: usize = cap[3].parse().unwrap(); + let mut current_crate: Vec; + { + let starting_stack = crate_vec.get_mut(starting_stack_index-1).unwrap(); + current_crate = starting_stack.split_off(starting_stack.len().saturating_sub(crates_moved)); + } + + let ending_stack = crate_vec.get_mut(ending_stack_index-1).unwrap(); + + ending_stack.append(&mut current_crate); + } + + let mut result = String::new(); + + for stack in crate_vec + { + result.push(*stack.last().unwrap()); + } + + println!("Final layout is {}", result.as_str()); +} + +fn main() { + //part1(); + part2(); }