From dff419d40a4a9c0d5ffced90367db9c504e238db Mon Sep 17 00:00:00 2001 From: EFanZh Date: Sat, 30 Nov 2024 12:44:21 +0800 Subject: [PATCH] Add problem 2347: Best Poker Hand --- src/lib.rs | 1 + .../brute_force.rs | 60 +++++++++++++++++++ src/problem_2347_best_poker_hand/mod.rs | 23 +++++++ 3 files changed, 84 insertions(+) create mode 100644 src/problem_2347_best_poker_hand/brute_force.rs create mode 100644 src/problem_2347_best_poker_hand/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 0ffafa9d..9617cce5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1738,6 +1738,7 @@ pub mod problem_2336_smallest_number_in_infinite_set; pub mod problem_2337_move_pieces_to_obtain_a_string; pub mod problem_2341_maximum_number_of_pairs_in_array; pub mod problem_2342_max_sum_of_a_pair_with_equal_sum_of_digits; +pub mod problem_2347_best_poker_hand; #[cfg(test)] mod test_utilities; diff --git a/src/problem_2347_best_poker_hand/brute_force.rs b/src/problem_2347_best_poker_hand/brute_force.rs new file mode 100644 index 00000000..0cd97c79 --- /dev/null +++ b/src/problem_2347_best_poker_hand/brute_force.rs @@ -0,0 +1,60 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + fn helper(ranks: &[i32; 5], suits: &[char; 5]) -> &'static str { + let first_suit = suits[0]; + + if suits[1..].iter().all(|&suit| suit == first_suit) { + return "Flush"; + } + + let mut counts = [0_u8; 13]; + let mut ranks_iter = ranks.iter().copied(); + + while let Some(rank) = ranks_iter.next() { + let count = &mut counts[(rank as usize).wrapping_sub(1)]; + + *count += 1; + + if *count == 2 { + for rank in ranks_iter { + let count = &mut counts[(rank as usize).wrapping_sub(1)]; + + if *count == 2 { + return "Three of a Kind"; + } + + *count += 1; + } + + return "Pair"; + } + } + + "High Card" + } + + pub fn best_hand(ranks: Vec, suits: Vec) -> String { + let (ranks, suits) = Option::zip(ranks.as_slice().try_into().ok(), suits.as_slice().try_into().ok()).unwrap(); + + Self::helper(ranks, suits).to_string() + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn best_hand(ranks: Vec, suits: Vec) -> String { + Self::best_hand(ranks, suits) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2347_best_poker_hand/mod.rs b/src/problem_2347_best_poker_hand/mod.rs new file mode 100644 index 00000000..cde3af61 --- /dev/null +++ b/src/problem_2347_best_poker_hand/mod.rs @@ -0,0 +1,23 @@ +pub mod brute_force; + +pub trait Solution { + fn best_hand(ranks: Vec, suits: Vec) -> String; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + (([13, 2, 3, 1, 9], "aaaaa"), "Flush"), + (([4, 4, 2, 4, 4], "daabc"), "Three of a Kind"), + (([10, 10, 2, 12, 9], "abcad"), "Pair"), + (([13, 12, 3, 4, 7], "adcbc"), "High Card"), + ]; + + for ((ranks, suits), expected) in test_cases { + assert_eq!(S::best_hand(ranks.to_vec(), suits.chars().collect()), expected); + } + } +}