From 3ed269fecf38ffdb007e2b074355ec57544b7bd5 Mon Sep 17 00:00:00 2001 From: EFanZh Date: Fri, 6 Sep 2024 21:34:16 +0800 Subject: [PATCH] Add problem 2162: Minimum Cost to Set Cooking Time --- src/lib.rs | 1 + .../mathematical.rs | 99 +++++++++++++++++++ .../mod.rs | 27 +++++ 3 files changed, 127 insertions(+) create mode 100644 src/problem_2162_minimum_cost_to_set_cooking_time/mathematical.rs create mode 100644 src/problem_2162_minimum_cost_to_set_cooking_time/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 77e8f875..9acd251a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1593,6 +1593,7 @@ pub mod problem_2155_all_divisions_with_the_highest_score_of_a_binary_array; pub mod problem_2156_find_substring_with_given_hash_value; pub mod problem_2160_minimum_sum_of_four_digit_number_after_splitting_digits; pub mod problem_2161_partition_array_according_to_given_pivot; +pub mod problem_2162_minimum_cost_to_set_cooking_time; pub mod problem_2164_sort_even_and_odd_indices_independently; pub mod problem_2165_smallest_value_of_the_rearranged_number; pub mod problem_2166_design_bitset; diff --git a/src/problem_2162_minimum_cost_to_set_cooking_time/mathematical.rs b/src/problem_2162_minimum_cost_to_set_cooking_time/mathematical.rs new file mode 100644 index 00000000..8c383ff6 --- /dev/null +++ b/src/problem_2162_minimum_cost_to_set_cooking_time/mathematical.rs @@ -0,0 +1,99 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + fn check_cost(mut current: u8, move_cost: u32, push_cost: u32, d0: u8, d1: u8, d2: u8, d3: u8) -> u32 { + let mut result = 0; + + let mut press = |digit| { + result += push_cost; + + if current != digit { + current = digit; + result += move_cost; + } + }; + + 'b4: { + 'b3: { + 'b2: { + if d0 == 0 { + if d1 == 0 { + if d2 == 0 { + break 'b4; + } + + break 'b3; + } + + break 'b2; + } + + press(d0); + } + + press(d1); + } + + press(d2); + } + + press(d3); + + result + } + + pub fn min_cost_set_time(start_at: i32, move_cost: i32, push_cost: i32, target_seconds: i32) -> i32 { + let (start_at, move_cost, push_cost, target_seconds) = ( + start_at as u8, + move_cost as u32, + push_cost as u32, + target_seconds as u16, + ); + + let minutes = (target_seconds / 60) as u8; + let seconds = (target_seconds % 60) as u8; + let mut m0 = minutes / 10; + let mut m1 = minutes % 10; + let mut s0 = seconds / 10; + let s1 = seconds % 10; + + let mut result = if target_seconds < 6000 { + Self::check_cost(start_at, move_cost, push_cost, m0, m1, s0, s1) + } else { + u32::MAX + }; + + if target_seconds >= 60 && s0 < 4 { + s0 += 6; + + if m1 == 0 { + m1 = 9; + m0 -= 1; + } else { + m1 -= 1; + } + + result = result.min(Self::check_cost(start_at, move_cost, push_cost, m0, m1, s0, s1)); + } + + result as _ + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn min_cost_set_time(start_at: i32, move_cost: i32, push_cost: i32, target_seconds: i32) -> i32 { + Self::min_cost_set_time(start_at, move_cost, push_cost, target_seconds) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2162_minimum_cost_to_set_cooking_time/mod.rs b/src/problem_2162_minimum_cost_to_set_cooking_time/mod.rs new file mode 100644 index 00000000..0b5d48f8 --- /dev/null +++ b/src/problem_2162_minimum_cost_to_set_cooking_time/mod.rs @@ -0,0 +1,27 @@ +pub mod mathematical; + +pub trait Solution { + fn min_cost_set_time(start_at: i32, move_cost: i32, push_cost: i32, target_seconds: i32) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + ((1, 2, 1, 600), 6), + ((0, 1, 2, 76), 6), + ((7, 220, 479, 6000), 2576), + ((7, 85276, 26772, 107), 336_144), + ((0, 1, 4, 9), 5), + ]; + + for ((start_at, move_cost, push_cost, target_seconds), expected) in test_cases { + assert_eq!( + S::min_cost_set_time(start_at, move_cost, push_cost, target_seconds), + expected, + ); + } + } +}