From adda315684189660f940d2209bfbf8f7f2e6e91b Mon Sep 17 00:00:00 2001 From: Andy Teijelo Date: Thu, 7 Dec 2023 18:38:45 -0500 Subject: [PATCH] day 7 part 2 --- src/main.rs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5fc27c6..dd38eef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,20 @@ enum HandType { } fn hand_type(hand: &str) -> HandType { + let counter_without_joker: Counter = hand.chars().filter(|c| *c != 'J').collect(); + if counter_without_joker.is_empty() { + // it's JJJJJ + return HandType::FiveOfAKind; + } + let hand = hand.replace( + 'J', + &counter_without_joker + .k_most_common_ordered(1) + .first() + .unwrap() + .0 + .to_string(), + ); let counter: Counter = hand.chars().collect(); if counter.keys().len() == 1 { return HandType::FiveOfAKind; @@ -54,7 +68,7 @@ fn hand_cmp_key(hand: &str) -> CmpKey { (HandType::OnePair, 2u8), (HandType::HighCard, 1u8), ]); - let card_strength: HashMap = "AKQJT98765432" + let card_strength: HashMap = "AKQT98765432J" .chars() .enumerate() .map(|(i, c)| (c, (14 - i) as u8)) @@ -92,12 +106,14 @@ mod tests { #[test] fn test_hand_types() { - assert_eq!(hand_type("AAAAA"), HandType::FiveOfAKind); - assert_eq!(hand_type("AA8AA"), HandType::FourOfAKind); - assert_eq!(hand_type("23332"), HandType::FullHouse); - assert_eq!(hand_type("TTT98"), HandType::ThreeOfAKind); - assert_eq!(hand_type("23432"), HandType::TwoPair); - assert_eq!(hand_type("A23A4"), HandType::OnePair); + assert_eq!(hand_type("AAAAJ"), HandType::FiveOfAKind); + assert_eq!(hand_type("AAAJJ"), HandType::FiveOfAKind); + assert_eq!(hand_type("AAJJJ"), HandType::FiveOfAKind); + assert_eq!(hand_type("T55J5"), HandType::FourOfAKind); + assert_eq!(hand_type("KTJJT"), HandType::FourOfAKind); + assert_eq!(hand_type("QQQJA"), HandType::FourOfAKind); + assert_eq!(hand_type("2J432"), HandType::ThreeOfAKind); + assert_eq!(hand_type("A23J4"), HandType::OnePair); assert_eq!(hand_type("23456"), HandType::HighCard); } }