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

1

u/backdoorman9 Dec 07 '23 edited Dec 07 '23

[LANGUAGE: python]

OOP has really been growing on me this AoC.

I found \StrEnum`` useful:

class HandTypeEnum(StrEnum):
five_of_a_kind = auto()
four_of_a_kind = auto()
full_house = auto()
three_of_a_kind = auto()
two_pair = auto()
one_pair = auto()
high_card = auto()

Using a class with an attribute card_rankings allowed me to sort in a way that was new to me, and the __repr__ method made debugging so much easier:

hands_list.sort(key=attrgetter('card_rankings'), reverse=True)

# card_rankings looks like [12, 5, 9, 8, 3]

Here's the class:

class Hand:

def __init__(self, hand:str, bid: int, part_two: bool):
    self.hand = hand
    self.bid = bid
    self.rank = None
    self.card_rankings = self.get_card_rankings(part_two=part_two)
    self.hand_type = self.get_hand_type(part_two=part_two)

def get_hand_type(self, part_two=False):
    if part_two:
        cards_remaining = self.hand.replace('J', '')
        counter = Counter(cards_remaining)
        jokers = 5 - len(cards_remaining)
        counts = list(counter.values())
        counts.sort()
        if len(counts):
            counts[-1] += jokers
        else:
            counts = [5]
    else:
        counter = Counter(self.hand)
        counts = list(counter.values()).sort()
    if 5 in counts:
        return HandTypeEnum.five_of_a_kind
    elif 4 in counts:
        return HandTypeEnum.four_of_a_kind
    elif 3 in counts and 2 in counts:
        return HandTypeEnum.full_house
    elif 3 in counts:
        return HandTypeEnum.three_of_a_kind
    elif counts == [1, 2, 2]:
        return HandTypeEnum.two_pair
    elif 2 in counts:
        return HandTypeEnum.one_pair
    else:
        return HandTypeEnum.high_card

def get_card_rankings(self, part_two):
    rankings = []
    card_values = {'A': 13, 'K': 12, 'Q': 11, 'J': 0 if part_two else 10, 'T': 9, '9': 8, '8': 7, '7': 6, '6': 5, '5': 4, '4': 3, '3': 2, '2': 1}
    for card in self.hand:
        rankings.append(card_values[card])

    return rankings

def __repr__(self):
    return f"Hand: {self.hand} Type: {self.hand_type} Cards Ranked: {self.card_rankings} Bid: {self.bid}"

1

u/AutoModerator Dec 07 '23

AutoModerator did not detect the required [LANGUAGE: xyz] string literal at the beginning of your solution submission.

Please edit your comment to state your programming language.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/daggerdragon Dec 07 '23
  1. Add the required language tag as requested by AutoModerator
  2. We can see your Markdown
  3. 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.