Solve Part 2

This commit is contained in:
apio 2022-12-09 10:56:30 +01:00
parent 1762cb4036
commit 5bf430a421
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -65,6 +65,7 @@ fn process_crates(crates_and_numbers: &str) -> Vec<Vec<char>>
result result
} }
#[allow(dead_code)]
fn part1() fn part1()
{ {
let (crates, procedures) = split_into_crates_and_procedures(DATA); let (crates, procedures) = split_into_crates_and_procedures(DATA);
@ -101,6 +102,40 @@ fn part1()
println!("Final layout is {}", result.as_str()); println!("Final layout is {}", result.as_str());
} }
fn main() { fn part2()
part1(); {
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<char>;
{
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();
} }