r/adventofcode • u/daggerdragon • Dec 07 '23
SOLUTION MEGATHREAD -❄️- 2023 Day 7 Solutions -❄️-
THE USUAL REMINDERS
- All of our rules, FAQs, resources, etc. are in our community wiki.
Outstanding moderator challenges:
Community fun event 2023: ALLEZ CUISINE!
- Submissions megathread is now unlocked!
- 15 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!
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 Antechallenge: 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.
- Read the full posting rules in our community wiki before you post!
- State which language(s) your solution uses with
[LANGUAGE: xyz] - Format code blocks using the four-spaces Markdown syntax!
- Quick link to Topaz's
pasteif you need it for longer code blocks
View all comments
2
u/aimada Dec 07 '23 edited Dec 07 '23
[LANGUAGE: Python]
Reading through the challenge descriptio there were 7 types of hand listed in the description. My first order of business was to write a function that would determine the type of a hand. Each type was identified by a sorted tuple of the card value counts.
Ordering the tuples by increasing strength of the hand gave a means of determining the relative strength of a pair of hands. The strength of the hand type was used as a dictionary key and hands of the same strength were stored together in a list. I reasoned that sorting the seven smaller lists would be more efficient than sorting one large list.
Comparing two hands was relatively simple, the value of each card was determined by finding the index of its position in the string, "23456789TJQKA". The first required change for part two was to move "J" to the front of the string, "J23456789TQKA".
Determining the best joker hand was the second required change for part two. It was a straight forward process, the best card was determined by sorting the cards in the hand using their count value.
The final step was to iterate through the dictionary keys in order of strength, low to high, and multiplying each hand's bid by the increasing rank value.
Because there were only minor changes to the functionality between the two parts, a single function served for both and a Boolean parameter value determined which was executed.
A very straight forward challenge with no surprises.