Skip to content

Commit

Permalink
Add problem 2162: Minimum Cost to Set Cooking Time
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Sep 6, 2024
1 parent 826374a commit 3ed269f
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
99 changes: 99 additions & 0 deletions src/problem_2162_minimum_cost_to_set_cooking_time/mathematical.rs
Original file line number Diff line number Diff line change
@@ -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::<super::Solution>();
}
}
27 changes: 27 additions & 0 deletions src/problem_2162_minimum_cost_to_set_cooking_time/mod.rs
Original file line number Diff line number Diff line change
@@ -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<S: Solution>() {
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,
);
}
}
}

0 comments on commit 3ed269f

Please sign in to comment.