day7: Explain theory a bit

This commit is contained in:
Tristan Daniël Maat 2023-12-08 19:22:09 +01:00
parent 5674535478
commit ceaf7c8735
Signed by: tlater
GPG key ID: 49670FD774E43268

View file

@ -87,6 +87,18 @@ fun typeJoker(cards: Hand): Int {
} }
val jokers = groups.remove('J') ?: 0 val jokers = groups.remove('J') ?: 0
// Jokers act as a kind of tie breaker here - similar to the full
// scenario, the label we want to add the joker to is the one with
// the highest card value *as well as* the highest type.
//
// So we sort it first by value (descending so the highest type
// gets i == 0), and second by the value of the card (so that if
// two hands have the same type the one with the higher card value
// wins).
//
// The hands are base 13 numbers, so multiplying the value puts it
// one digit ahead of the card value.
return groups.entries.sortedByDescending { 13 * it.value + cardValueJoker(it.key) }.foldIndexed( return groups.entries.sortedByDescending { 13 * it.value + cardValueJoker(it.key) }.foldIndexed(
0 0
) { i, acc, entry -> ) { i, acc, entry ->