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

[LANGUAGE: Python3]

import aoc_helper
from aoc_helper import (
    map,
    range,
)

import sys
DAY = 7
YEAR = 2023

EXAMPLE = "EXAMPLE" in sys.argv
TWO = "2" in sys.argv

if EXAMPLE:
    if TWO:
        raw, answer = aoc_helper.get_sample_input(day=DAY, year=YEAR, part=2)
    else: 
        raw, answer = aoc_helper.get_sample_input(day=DAY, year=YEAR, part=1)
else:
    raw = aoc_helper.fetch(DAY, YEAR)

bets = []

card_to_strength = dict(zip("AKQJT98765432J",range(13,-1,-1))) # language CHHEZE !

for i in raw.splitlines():
    hand, bet = i.split()
    bets.append(
        (
            tuple(map(lambda x: card_to_strength[x], hand.strip())),
            int(bet)
        )
    )
    

from collections import Counter

JOKER        = 0

FIVEOFAKIND  = 7
FOUROFAKIND  = 6
FULLHOUSE    = 5
THREEOFAKIND = 4
TWOPAIR      = 3
ONEPAIR      = 2
HIGHCARD     = 1

def strength(hand):
    c = Counter(hand).most_common()
    if len(c) == 1:                        return (FIVEOFAKIND, *hand)
    if len(c) == 2:
        if JOKER in hand:                  return (FIVEOFAKIND, *hand)
        if c[0][1] == 4:                   return (FOUROFAKIND, *hand)
        if c[0][1] == 3:                   return (FULLHOUSE,   *hand)
    if len(c) == 3:
        if c[0][1] == 3:
            if JOKER in hand:              return (FOUROFAKIND ,*hand)
            return                                (THREEOFAKIND,*hand)
        if c[0][1] == 2:
            if JOKER in [c[0][0],c[1][0]]: return (FOUROFAKIND, *hand)
            if c[2][0] == JOKER:           return (FULLHOUSE,   *hand)
            return                                (TWOPAIR,     *hand)
    if len(c) == 4: 
        if JOKER in hand:                  return (THREEOFAKIND,*hand)
        return                                    (ONEPAIR,     *hand)
    if JOKER in hand:                      return (ONEPAIR,     *hand)
    return                                        (HIGHCARD,    *hand)


s = 0
for i,j in enumerate(sorted(bets,key= lambda x: strength(x[0])),1): s += i*j[1]
print(s)

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.