Solve Part 2
This commit is contained in:
parent
1762cb4036
commit
5bf430a421
39
src/main.rs
39
src/main.rs
@ -65,6 +65,7 @@ fn process_crates(crates_and_numbers: &str) -> Vec<Vec<char>>
|
||||
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<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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user