-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2391: Minimum Amount of Time to Collect Garbage
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/problem_2391_minimum_amount_of_time_to_collect_garbage/iterative.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::iter; | ||
|
||
impl Solution { | ||
pub fn garbage_collection(garbage: Vec<String>, travel: Vec<i32>) -> 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<String>, travel: Vec<i32>) -> i32 { | ||
Self::garbage_collection(garbage, travel) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/problem_2391_minimum_amount_of_time_to_collect_garbage/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn garbage_collection(garbage: Vec<String>, travel: Vec<i32>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
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, | ||
); | ||
} | ||
} | ||
} |