1
0
Fork 0

day(07): unify the hand type deduction

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-12-07 10:29:24 +01:00
parent 03e59ecf8f
commit b582bf33d9
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -85,40 +85,23 @@ impl Hand {
fn hand_type_no_joker(&self) -> HandType { fn hand_type_no_joker(&self) -> HandType {
let freqs = self.cards.iter().counts(); 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; if freqs[0] == 5 {
let mut triplets = 0; HandType::FiveOfAKind
} else if freqs[0] == 4 {
for &value in freqs.values() { HandType::FourOfAKind
match value { } else if freqs[0] == 3 && freqs[1] == 2 {
5 => { HandType::FullHouse
return HandType::FiveOfAKind; } else if freqs[0] == 3 {
} HandType::ThreeOfAKind
4 => { } else if freqs[0] == 2 && freqs[1] == 2 {
return HandType::FourOfAKind; HandType::TwoPair
} } else if freqs[0] == 2 {
3 => { HandType::OnePair
triplets += 1; } else {
} HandType::HighCard
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,
} }
} }