diff --git a/day7/day7.kt b/day7/day7.kt index 33de794..863bc37 100644 --- a/day7/day7.kt +++ b/day7/day7.kt @@ -87,6 +87,18 @@ fun typeJoker(cards: Hand): Int { } 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( 0 ) { i, acc, entry ->