day(07): unify the hand type deduction
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
03e59ecf8f
commit
b582bf33d9
1 changed files with 16 additions and 33 deletions
|
@ -85,40 +85,23 @@ impl Hand {
|
|||
|
||||
fn hand_type_no_joker(&self) -> HandType {
|
||||
let freqs = self.cards.iter().counts();
|
||||
let mut freqs = freqs.values().cloned().collect_vec();
|
||||
freqs.sort_by_key(|&c| Reverse(c));
|
||||
|
||||
let mut pairs = 0;
|
||||
let mut triplets = 0;
|
||||
|
||||
for &value in freqs.values() {
|
||||
match value {
|
||||
5 => {
|
||||
return HandType::FiveOfAKind;
|
||||
}
|
||||
4 => {
|
||||
return HandType::FourOfAKind;
|
||||
}
|
||||
3 => {
|
||||
triplets += 1;
|
||||
}
|
||||
2 => {
|
||||
pairs += 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
if pairs == 1 && triplets == 1 {
|
||||
return HandType::FullHouse;
|
||||
}
|
||||
|
||||
if triplets > 0 {
|
||||
return HandType::ThreeOfAKind;
|
||||
}
|
||||
|
||||
match pairs {
|
||||
2 => HandType::TwoPair,
|
||||
1 => HandType::OnePair,
|
||||
_ => HandType::HighCard,
|
||||
if freqs[0] == 5 {
|
||||
HandType::FiveOfAKind
|
||||
} else if freqs[0] == 4 {
|
||||
HandType::FourOfAKind
|
||||
} else if freqs[0] == 3 && freqs[1] == 2 {
|
||||
HandType::FullHouse
|
||||
} else if freqs[0] == 3 {
|
||||
HandType::ThreeOfAKind
|
||||
} else if freqs[0] == 2 && freqs[1] == 2 {
|
||||
HandType::TwoPair
|
||||
} else if freqs[0] == 2 {
|
||||
HandType::OnePair
|
||||
} else {
|
||||
HandType::HighCard
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue