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/fullmetalalch Dec 07 '23

[Language: Python] Gitlab

Today wasn't too bad! Just took some time to think through the hand evaluation logic. Going to peruse other solutions to see if there is a better way to handle jokers than hard-coded conditionals everywhere.

@cache
def evaluate_hand_type(cards: str) -> HandType:
  card_counts = collections.defaultdict(lambda: 0)
  
  for card in cards:
    card_counts[card] += 1
    
  if len(card_counts) == 1:
    return HandType.QUINT
  elif len(card_counts) == 2:
    cnts = card_counts.values()
    if 1 in cnts or 4 in cnts:
      return HandType.QUAD
    return HandType.FULL_HOUSE
  elif len(card_counts) == 3:
    cnts = card_counts.values()
    if 3 in cnts:
      return HandType.TRIPS
    return HandType.TWO_PAIR
  elif len(card_counts) == 4:
    return HandType.PAIR
  return HandType.HIGH_CARD


@cache
def evaluate_hand_type_with_joker(cards: str) -> HandType:
  card_counts = collections.defaultdict(lambda: 0)
  
  for card in cards:
    card_counts[card] += 1
    
  if len(card_counts) == 1:
    return HandType.QUINT
  elif len(card_counts) == 2:
    if card_counts["J"] > 0:
      return HandType.QUINT
    cnts = card_counts.values()
    if 1 in cnts or 4 in cnts:
      return HandType.QUAD
    return HandType.FULL_HOUSE
  elif len(card_counts) == 3:
    cnts = card_counts.values()
    if card_counts["J"] in (2, 3) or (card_counts["J"] == 1 and 3 in cnts):
      return HandType.QUAD
    elif card_counts["J"] == 1:
      return HandType.FULL_HOUSE
    if 3 in cnts:
      return HandType.TRIPS
    return HandType.TWO_PAIR
  elif len(card_counts) == 4:
    if card_counts["J"] > 0:
      return HandType.TRIPS
    return HandType.PAIR
  elif card_counts["J"] == 1:
    return HandType.PAIR
  return HandType.HIGH_CARD


CARD_VALUE = {"A": 14, "K": 13, "Q": 12, "J": 11, "T": 10}
for i in range(2, 10):
  CARD_VALUE[str(i)] = i
  
@dataclass
class Hand:
  cards: str
  bet: int
  evaluate_type: Callable[[str], HandType]
  
  @property
  def type(self) -> HandType:
    return self.evaluate_type(self.cards)
  
  def __lt__(self, other: 'Hand') -> bool:
    if self.type != other.type:
      return self.type < other.type
    
    for card_a, card_b in zip(self.cards, other.cards):
      if card_a != card_b:
        return CARD_VALUE[card_a] < CARD_VALUE[card_b]
      
    return False  # cards are equal

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 only that external link to your code.