Day 5 completed, finally haha
This commit is contained in:
parent
f2836e45e1
commit
2a9e8eab51
4 changed files with 542 additions and 3 deletions
|
@ -38,4 +38,9 @@ fn main() {
|
|||
let max_part1 = prob5::solve_part1(file_path);
|
||||
println!("Solution for problem 5 part 1: {max_part1}");
|
||||
}
|
||||
{
|
||||
let file_path = "prob5_part2.txt";
|
||||
let max_part1 = prob5::solve_part2(file_path);
|
||||
println!("Solution for problem 5 part 2: {max_part1}");
|
||||
}
|
||||
}
|
||||
|
|
28
src/prob5.rs
28
src/prob5.rs
|
@ -1,5 +1,6 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
use itertools::Itertools;
|
||||
use nom::{
|
||||
branch::alt,
|
||||
bytes::complete::{tag, take, take_while1},
|
||||
|
@ -139,9 +140,30 @@ pub fn solve_part1(file_path: &str) -> String {
|
|||
.push_front(tmp);
|
||||
});
|
||||
});
|
||||
let mut answer = String::new();
|
||||
|
||||
crate_columns
|
||||
.iter()
|
||||
.for_each(|item| answer.push(*item.front().unwrap_or_else(|| panic!(""))));
|
||||
answer
|
||||
.map(|item| item.front().unwrap())
|
||||
.join("")
|
||||
}
|
||||
|
||||
pub fn solve_part2(file_path: &str) -> String {
|
||||
let content = std::fs::read_to_string(file_path).unwrap_or_else(|err| panic!("{err}"));
|
||||
let (mut crate_columns, instructions) = parser(&content);
|
||||
instructions.iter().for_each(|ins| {
|
||||
let mut test = vec!['\0'; ins.quantity];
|
||||
(0..ins.quantity).for_each(|i| {
|
||||
test[i] = crate_columns[ins.src]
|
||||
.pop_front()
|
||||
.unwrap_or_else(|| panic!("Could not pop front the column {}", ins.src));
|
||||
//crate_columns[ins.dst].push_front(tmp);
|
||||
});
|
||||
test.reverse();
|
||||
test.iter_mut()
|
||||
.for_each(|tmp| crate_columns[ins.dst].push_front(*tmp));
|
||||
});
|
||||
crate_columns
|
||||
.iter()
|
||||
.map(|item| item.front().unwrap())
|
||||
.join("")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue