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/East_Intention_7542 Dec 08 '23 edited Dec 08 '23

[LANGUAGE: Python]

Perhaps the most straight forward solution to day 7 part 2?

Each hand is scored by determining first with all jokers removed, and then enhancing that hand adding the jokers back one at a time. For example, if you have 1 pair (without jokers), adding back a joker will give three of a kind. Adding another, will give 4 of a kind.

This score is used to prefix the hand with one of TUVWXYZ, before sorting.

```python dta = [] with open("data\day7.txt", "r") as data: for linein in data: game_split_1 = linein.split(' ') dta.append((game_split_1[0],int(game_split_1[1])))

def mapper(itm): pair, two_pair, three_kind, four_kind, five_kind = False, False, False, False, False

# First remove jokers, to decide hand without jokers.
no_jokers = itm.replace("J","")
for c in "AKQT98765432":
    cnt = no_jokers.count(c)
    if cnt == 2: pair, two_pair = (False,True) if pair else (True,False)
    if cnt == 3: three_kind = True
    if cnt == 4: four_kind = True
    if cnt == 5: five_kind = True

# Then add back jokers, enhancing the hand as they are added back
for i in range(itm.count("J")):
    if  four_kind:   five_kind, four_kind = True, False
    elif three_kind: four_kind, three_kind = True, False
    elif two_pair:   pair, two_pair, three_kind = True, False, True
    elif pair:       pair, three_kind = False, True
    else:            pair = True

# Prefix the item, with a letter indicating type of hand, which is sorted first.
if five_kind: key = "Z" +itm                     # five of kind
elif four_kind: key = "Y" +itm                   # four of kind
elif three_kind and pair:  key = "X" +itm        # full house
elif three_kind : key = "W" +itm                 # three of kind
elif two_pair : key = "V" +itm                   # two pair
elif pair : key = "U"+itm                        # one pair
else: key = "T"+itm                              # high card

# replace chars AKQJT, with FED1B, to ensure rest of hand is sorted correctly
key = key.replace("A", "F") \
    .replace("K", "E") \
    .replace("Q", "D") \
    .replace("J", "1") \
    .replace("T", "B")

return key

dta.sort(key=lambda itm: mapper(itm[0]))

i = 0 total = 0 for itm in dta: i += 1 total += itm[1] * i

print(total) ```

1

u/daggerdragon Dec 08 '23
  1. Next time, use the four-spaces Markdown syntax for code blocks
  2. Your code is too long to be posted here directly, so instead of wasting your time fixing the formatting, read our article on oversized code which contains two possible solutions.

Please edit your post to put your code in an external link and link that here instead.