Skip to content
advent of code 2023

day 07

camel cards

approach

hands are sorted by a key: a hand-type rank (0 for high card up to 6 for five of a kind) followed by the tuple of card values. the type comes from a Counter of the cards, using the number of distinct cards and the largest count. the winnings are each bid times its position in the sorted list.

part 2 makes J the weakest card and treats it as a joker: jokers are removed from the count and added to the most common remaining card, then the same type rules apply. a hand of five jokers is handled separately as five of a kind.

the trick

a joker always does best by joining the most common other card.

python
hand_s, bid = hand_bid
hand_ord = tuple(map(lambda x: order[x], hand_s))
jokers = hand_s.count("J")

if jokers == 5:
    # this is unbeatable
    # i though maybe this
    # was unbeatable
    # turns out nope
    return (6, ) + hand_ord

hand = Counter(c for c in hand_s if c != "J")
mc = [[c, f] for c, f in hand.most_common()]
mc[0][1] += jokers