From cbfc3a4dfcf035a1a9a643811fd54b960665fb1f Mon Sep 17 00:00:00 2001 From: Luke Else Date: Thu, 7 Dec 2023 18:57:05 +0000 Subject: [PATCH] Day 7 Part 1 complete -> Didn't realise that sorting the hands of the same type required using an unsorted list :) --- src/SOLUTUION | 3 +++ src/solutions/day07.rs | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 src/SOLUTUION diff --git a/src/SOLUTUION b/src/SOLUTUION new file mode 100644 index 0000000..7ad19b2 --- /dev/null +++ b/src/SOLUTUION @@ -0,0 +1,3 @@ +This one was super fun - I really enjoyed it. I defined two enumerated types: one ordered lowest to highest for the card ranks, and another ranked lowest to highest for the hand ranks. The real meat of the code is in function that evaluates the rank of a hand: I used a hash map to build a histogram of each hand, then used the card counts to determine what kind of hand I had for Part 1. I then fed this into a comparison function that compared the ranking of two hands to determine less, equal, or greater, and then fed that to the standard sort function to rank the entire vector of hands and their associated bids. It was then trivial to iterate over the sorted vector to multiply the ranking of each hand with its bid and add it to the sum. + +Part 2 was slightly harder, just reworked the card ranking to make the wildcard the lowest ranked card, then reworked the logic in the hand ranking function to take into account the presence of any wild cards in the hand. \ No newline at end of file diff --git a/src/solutions/day07.rs b/src/solutions/day07.rs index 2fb9a13..7d88aab 100644 --- a/src/solutions/day07.rs +++ b/src/solutions/day07.rs @@ -79,8 +79,8 @@ impl Day07 { }); } - hand.sort(); - hands.push((hand.iter().rev().map(|c| *c).collect(), stake.parse()?)); + hands.push((hand, stake.parse()?)); + // hands.push((hand.iter().rev().map(|c| *c).collect(), stake.parse()?)); } Ok(hands) }