Day 7 Part 1 complete -> Didn't realise that sorting the hands of the same type required using an unsorted list :)

This commit is contained in:
Luke Else 2023-12-07 18:57:05 +00:00
parent 12fe856e43
commit cbfc3a4dfc
2 changed files with 5 additions and 2 deletions

3
src/SOLUTUION Normal file
View File

@ -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.

View File

@ -79,8 +79,8 @@ impl Day07 {
}); });
} }
hand.sort(); hands.push((hand, stake.parse()?));
hands.push((hand.iter().rev().map(|c| *c).collect(), stake.parse()?)); // hands.push((hand.iter().rev().map(|c| *c).collect(), stake.parse()?));
} }
Ok(hands) Ok(hands)
} }