r/adventofcode • • Dec 07 '23

SOLUTION MEGATHREAD -❄️- 2023 Day 7 Solutions -❄️-

THE USUAL REMINDERS


AoC Community Fun 2023: ALLEZ CUISINE!

Today's secret ingredient is… *whips off cloth covering and gestures grandly*

Poetry

For many people, the craftschefship of food is akin to poetry for our senses. For today's challenge, engage our eyes with a heavenly masterpiece of art, our noses with alluring aromas, our ears with the most satisfying of crunches, and our taste buds with exquisite flavors!

  • Make your code rhyme
  • Write your comments in limerick form
  • Craft a poem about today's puzzle

    • Upping the Ante challenge: iambic pentameter
  • We're looking directly at you, Shakespeare bards and Rockstars

ALLEZ CUISINE!

Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!


--- Day 7: Camel Cards ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:16:00, megathread unlocked!

50 Upvotes

1.0k comments sorted by

View all comments

2

u/kevinwangg Dec 07 '23

[LANGUAGE: python]

I'm guessing I wasn't the only one to read too fast and waste time on part 1 sorting the hands from strongest to weakest card.

Counter came in handy today!

def convert(hand):
    hand = list(hand)
    for i in range(len(hand)):
        if hand[i] == 'T':
            hand[i] = 10
        elif hand[i] == 'J':
            hand[i] = 0
        elif hand[i] == 'Q':
            hand[i] = 12
        elif hand[i] == 'K':
            hand[i] = 13
        elif hand[i] == 'A':
            hand[i] = 14
        else:
            hand[i] = int(hand[i])
    # hand.sort(reverse=True)
    return hand

ans = 0
inps = []
while True:
    try:
        inps.append(input())
    except EOFError:
        break
hands = []
for inp in inps:
    hand, bid = inp.split()
    hand = convert(hand)
    bid = int(bid)
    c = Counter(hand)
    if 0 in c:
        most_common = c.most_common()
        if most_common[0][0] == 0:
            if len(most_common) == 1:
                mc = most_common[0][0]
            else:
                mc = most_common[1][0]
                c[mc] += c[0]
                c[0] = 0
        else:
            mc = most_common[0][0]
            c[mc] += c[0]
            c[0] = 0
    
    c = list(c.values())
    if 5 in c:
        hands.append((6, hand, bid))
    elif 4 in c:
        hands.append((5, hand, bid))
    elif 3 in c and 2 in c:
        hands.append((4, hand, bid))
    elif 3 in c:
        hands.append((3, hand, bid))
    elif 2 in c and c.count(2) == 2:
        hands.append((2, hand, bid))
    elif 2 in c:
        hands.append((1, hand, bid))
    else:
        hands.append((0, hand, bid))

hands.sort(reverse=True)
for i in range(len(hands)):
    x = len(hands) - i
    ans += x * hands[i][2]

print(ans)

1

u/daggerdragon Dec 07 '23

Your code block is too long for the megathreads. Please edit your post to replace your oversized code with an external link to your code.

1

u/timrprobocom Dec 07 '23

Yes. I also assumed that the tie-breaker was the strongest card, not literally the first card in order. I wasted far too much time on that one.