Derive is amazing
Today's Day of Code (Day 7) was about sorting Hands of Cards by how good they were with Rules similar to Poker, but thanks to derive, all you have to do in Rust is the parsing and something like:
#[derive(PartialEq, PartialOrd, Eq, Ord)]
enum Card {
Number(u32),
Jack,
Queen,
King,
Ace,
}
#[derive(PartialEq, PartialOrd, Eq, Ord)]
enum Hand {
HighCard(Vec<Card>),
OnePair(Vec<Card>),
TwoPair(Vec<Card>),
ThreeOfAKind(Vec<Card>),
FullHouse(Vec<Card>),
FourOfAKind(Vec<Card>),
FiveOfAKind(Vec<Card>),
}
and you can now just sort() the parsed input and it will consider:
- any FiveOfAKind over any FullHouse etc.
- correctly use the first card as a tie breaker
- continuing down the cards if the tie persists
- have Numbers as the least valuable cards, while still sorting them by their Value
For Part 2 I just had to move some lines and add 1 extra line of logic, but that was done within a minute.
I'm used to how easy you can lexicographically compare tuples and slices in Rust, but here everything came together to have Rust solve almost everything itself :)