diff --git a/src/lib.rs b/src/lib.rs index 423d52ea..b082b5d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1780,6 +1780,7 @@ pub mod problem_2385_amount_of_time_for_binary_tree_to_be_infected; pub mod problem_2386_find_the_k_sum_of_an_array; pub mod problem_2389_longest_subsequence_with_limited_sum; pub mod problem_2390_removing_stars_from_a_string; +pub mod problem_2391_minimum_amount_of_time_to_collect_garbage; #[cfg(test)] mod test_utilities; diff --git a/src/problem_2391_minimum_amount_of_time_to_collect_garbage/iterative.rs b/src/problem_2391_minimum_amount_of_time_to_collect_garbage/iterative.rs new file mode 100644 index 00000000..5bfc771b --- /dev/null +++ b/src/problem_2391_minimum_amount_of_time_to_collect_garbage/iterative.rs @@ -0,0 +1,68 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +use std::iter; + +impl Solution { + pub fn garbage_collection(garbage: Vec, travel: Vec) -> i32 { + let mut total_pickup_time = 0; + let mut total_metal_travel_time = 0; + let mut total_plastic_travel_time = 0; + let mut total_glass_travel_time = 0; + let mut total_travel_time = 0; + + garbage + .iter() + .zip(iter::once(0).chain(travel)) + .for_each(|(garbage, travel_cost)| { + total_pickup_time += garbage.len() as i32; + + let mut state = 0; + + for c in garbage.bytes() { + state |= match c { + b'M' => 1, + b'P' => 2, + _ => 4, + }; + + if state == 7 { + break; + } + } + + total_travel_time += travel_cost; + + if state & 1 != 0 { + total_metal_travel_time = total_travel_time; + } + + if state & 2 != 0 { + total_plastic_travel_time = total_travel_time; + } + + if state & 4 != 0 { + total_glass_travel_time = total_travel_time; + } + }); + + total_pickup_time + total_metal_travel_time + total_plastic_travel_time + total_glass_travel_time + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn garbage_collection(garbage: Vec, travel: Vec) -> i32 { + Self::garbage_collection(garbage, travel) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2391_minimum_amount_of_time_to_collect_garbage/mod.rs b/src/problem_2391_minimum_amount_of_time_to_collect_garbage/mod.rs new file mode 100644 index 00000000..23733a17 --- /dev/null +++ b/src/problem_2391_minimum_amount_of_time_to_collect_garbage/mod.rs @@ -0,0 +1,24 @@ +pub mod iterative; + +pub trait Solution { + fn garbage_collection(garbage: Vec, travel: Vec) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + ((&["G", "P", "GP", "GG"] as &[_], &[2, 4, 3] as &[_]), 21), + ((&["MMM", "PGM", "GP"], &[3, 10]), 37), + ]; + + for ((garbage, travel), expected) in test_cases { + assert_eq!( + S::garbage_collection(garbage.iter().copied().map(str::to_string).collect(), travel.to_vec()), + expected, + ); + } + } +}