-
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.
- Loading branch information
Showing
3 changed files
with
84 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
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,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<i32>, suits: Vec<char>) -> 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<i32>, suits: Vec<char>) -> String { | ||
Self::best_hand(ranks, suits) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
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,23 @@ | ||
pub mod brute_force; | ||
|
||
pub trait Solution { | ||
fn best_hand(ranks: Vec<i32>, suits: Vec<char>) -> String; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
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); | ||
} | ||
} | ||
} |