From 1275d5b7385a8c446fcebdd6c071e697553c9f66 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Thu, 7 Dec 2023 10:40:09 +0100 Subject: [PATCH] day(07): use match Signed-off-by: Matej Focko --- src/bin/day07.rs | 48 ++++++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/src/bin/day07.rs b/src/bin/day07.rs index 2efe44f..2d44a17 100644 --- a/src/bin/day07.rs +++ b/src/bin/day07.rs @@ -88,20 +88,14 @@ impl Hand { let mut freqs = freqs.values().cloned().collect_vec(); freqs.sort_by_key(|&c| Reverse(c)); - 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 + match freqs[..] { + [5, ..] => HandType::FiveOfAKind, + [4, ..] => HandType::FourOfAKind, + [3, 2, ..] => HandType::FullHouse, + [3, ..] => HandType::ThreeOfAKind, + [2, 2] => HandType::TwoPair, + [2, ..] => HandType::OnePair, + _ => HandType::HighCard, } } @@ -113,20 +107,18 @@ impl Hand { let mut freqs = freqs.values().cloned().collect_vec(); freqs.sort_by_key(|&c| Reverse(c)); - if jokers == 5 || freqs[0] + jokers == 5 { - HandType::FiveOfAKind - } else if freqs[0] + jokers == 4 { - HandType::FourOfAKind - } else if freqs[0] + jokers == 3 && freqs[1] == 2 { - HandType::FullHouse - } else if freqs[0] + jokers == 3 { - HandType::ThreeOfAKind - } else if freqs[0] == 2 && freqs[1] == 2 { - HandType::TwoPair - } else if freqs[0] + jokers == 2 { - HandType::OnePair - } else { - HandType::HighCard + if jokers == 5 { + return HandType::FiveOfAKind; + } + + match freqs[..] { + [n, ..] if n + jokers == 5 => HandType::FiveOfAKind, + [n, ..] if n + jokers == 4 => HandType::FourOfAKind, + [n, 2, ..] if n + jokers == 3 => HandType::FullHouse, + [n, ..] if n + jokers == 3 => HandType::ThreeOfAKind, + [2, 2, ..] => HandType::TwoPair, + [n, ..] if n + jokers == 2 => HandType::OnePair, + _ => HandType::HighCard, } } }