From f2ec4b3ba96685a8ba667b17b03d024eb78db13a Mon Sep 17 00:00:00 2001 From: Luke Else Date: Thu, 7 Dec 2023 20:45:35 +0000 Subject: [PATCH] Day 07 Part 2 complete (Ignore the messy code!) --- src/solutions/day07.rs | 88 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 6 deletions(-) diff --git a/src/solutions/day07.rs b/src/solutions/day07.rs index 7d88aab..ac171f6 100644 --- a/src/solutions/day07.rs +++ b/src/solutions/day07.rs @@ -20,7 +20,7 @@ impl Solution for Day07 { &self, input: &mut Vec, ) -> Result, Box> { - let hands = self.get_hands_from_string(input)?; + let hands = self.get_hands_from_string(input, false)?; let mut totals = vec![]; for hand in hands.iter() { @@ -37,7 +37,6 @@ impl Solution for Day07 { let mut score: u64 = 0; for (i, cards) in totals.iter().enumerate() { - println!("{} * {}, {:?} {}", i + 1, cards.1, cards.0, cards.2); score += (cards.1 * (i + 1) as u32) as u64; } Ok(Box::new(score)) @@ -45,9 +44,34 @@ impl Solution for Day07 { fn part2( &self, - _input: &mut Vec, + input: &mut Vec, ) -> Result, Box> { - Ok(Box::new("Ready")) + let hands = self.get_hands_from_string(input, true)?; + let mut totals = vec![]; + + for hand in hands.iter() { + let adapted_hand = self.replace_wildcard(&hand.0)?; + totals.push(( + hand.0.clone(), + hand.1, + self.get_hand_type(&adapted_hand)? as u32, + )); + println!("{:?}", hand); + } + + totals.sort_by(|a, b| { + if a.2 == b.2 { + a.0.partial_cmp(&b.0).unwrap() + } else { + (a.2 as u32).cmp(&(b.2 as u32)) + } + }); + + let mut score: u64 = 0; + for (i, cards) in totals.iter().enumerate() { + score += (cards.1 * (i + 1) as u32) as u64; + } + Ok(Box::new(score)) } fn get_day(&self) -> u8 { @@ -59,6 +83,7 @@ impl Day07 { fn get_hands_from_string( &self, input: &mut Vec, + wildcard: bool, ) -> Result, u32)>, Box> { let mut hands = vec![]; @@ -71,7 +96,13 @@ impl Day07 { for i in card.chars() { hand.push(match i { 'T' => 10, - 'J' => 11, + 'J' => { + if wildcard { + 0 + } else { + 11 + } + } 'Q' => 12, 'K' => 13, 'A' => 14, @@ -128,6 +159,51 @@ impl Day07 { // High Card Ok(Hands::HC) } + + fn replace_wildcard<'a>( + &self, + input: &Vec, + ) -> Result, Box> { + let mut map_cards = HashMap::new(); + + for c in input.iter() { + map_cards.entry(c).and_modify(|c| *c += 1).or_insert(1u32); + } + + // Get the wildcard count from the map and replace with the most common card + let wc_count = *map_cards.get(&0).unwrap_or(&0); + + // Remove wildcards as we have already + map_cards.remove_entry(&0); + + // Find the maximum frequency + let max_frequency = map_cards.values().cloned().max().unwrap_or(0); + + // Filter numbers with the maximum frequency + let most_frequenct_cards: Vec = map_cards + .clone() + .into_iter() + .filter(|&(_, frequency)| frequency == max_frequency) + .map(|(number, _)| *number) + .collect::>(); + + if most_frequenct_cards.len() == 0 { + return Ok(input.clone()); + } + + map_cards.entry(&most_frequenct_cards[0]).and_modify(|v| { + *v += wc_count; + }); + + let result: Vec = map_cards + .into_iter() + .flat_map(|(key, count)| vec![*key; count as usize]) + .collect(); + + println!("Replaced: {:?}", result); + + Ok(result) + } } /// Test from puzzle input @@ -167,6 +243,6 @@ mod test { .unwrap() .to_string(); - assert_eq!(answer, "Ready"); + assert_eq!(answer, "5905"); } }