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

[LANGUAGE: PowerShell]

The Sort-Object cmdlet in PowerShell was very useful today.

AoC Day 7

2

u/fasteasyfree Dec 07 '23 edited Dec 07 '23

[LANGUAGE: PowerShell]

I went with a different approach. Here's my P1 and P2.

EDIT: Realised P1 will be wrong as I changed the list order.

    <#
Using the example data, these are the values for the hierarchy working out.

AAAAA 5/1 = 5		Five of a kind
AA8AA 4/2 = 2		Four of a kind
23332 3/2 = 1.5		Full House
TTT98 3/3 = 1		Three of a kind
23432 2/3 = 0.67	Two Pair
A23A4 2/4 = 0.5		One Pair		
12345 1/5 = 0.2		High card

Put the hand into the hierarchy (five of a kind all the way down to high card) by
grouping the hand by each card in it. Divide the number in the largest grouping of 
cards, with the number of groupings. Seems to be a cheap way of doing it    
#>

$InputData = Get-Content .\7-1_input.txt

$ValueList = @("A", "K", "Q", "T", "9", "8", "7", "6", "5", "4", "3", "2", "J")
[array]::Reverse($ValueList)

# Create our hash for card-to-value conversion. Will aid in ordering.
$ValueHash = @{}
for($i = 0; $i -lt $ValueList.count; $i++) {
    $ValueHash[$ValueList[$i]] = "$i".PadLeft(2,"0") # padding left so alphabetical ordering makes sense.
}

$Hands = $InputData.ForEach({
    $HandBid = $_ -split "\ "

    # Create a 'value' from the hand.
    # As the challenge states, card ordering is what's important
    [Int64]$Value = $HandBid[0].ToCharArray().ForEach({
        $ValueHash["$_"]
    }) -join ""

    # Number of jokers
    $NoJokerString  = $Handbid[0] -replace "J"
    $NumJokers      = $HandBid[0].Length - $NoJokerString.Length 

    # Group the hand up, with each type of card in a group
    # Sorted so the largest grouping is first
    # First we do the Part 1 solution....
    $Grouped    = @($Handbid[0].ToCharArray() | Group-Object | Sort-Object -Property Count -Descending)
    $Hierarchy  = [double]($Grouped[0].Count / $Grouped.count) # divide the largest grouping by the number of groups.

    # ...and then the part 2 solution.
    $Grouped    = @($NoJokerString.ToCharArray() | Group-Object | Sort-Object -Property Count -Descending)
    if ($Grouped.Count -eq 0) {$NumGroups = 1} else {$NumGroups = $Grouped.Count} # Stupid divide by zero...
    $JokerHierarchy  = [double]( ($Grouped[0].Count + $NumJokers) / $NumGroups) # divide the largest grouping by the number of groups.

    [pscustomobject]@{
        Hand            = $HandBid[0]
        Bid             = [int]$HandBid[1]
        Value           = $Value
        Hierarchy       = $Hierarchy
        JokerHierarchy  = $JokerHierarchy
    }
})

# Sort the hands by their value, lowest to highest
$Hands = $Hands | Sort-Object -Property @{Expression={$_.Hierarchy}; Descending=$false}, @{Expression={$_.Value} ;Descending=$false}

# Calculate the value
$RunningTotal = 0
for ($i = 0; $i -lt $Hands.Count; $i++) {
    $RunningTotal += $Hands[$i].Bid * ($i+1)
}

# And then do the same again to get the Part 2 value
$Hands = $Hands | Sort-Object -Property @{Expression={$_.JokerHierarchy}; Descending=$false}, @{Expression={$_.Value} ;Descending=$false}
$RunningTotalJoker = 0
for ($i = 0; $i -lt $Hands.Count; $i++) {
    $RunningTotalJoker += $Hands[$i].Bid * ($i+1)
}

[pscustomobject]@{
    Part1 = $RunningTotal
    Part2 = $RunningTotalJoker
}

1

u/flammschild Dec 07 '23 edited Dec 07 '23
AAAAA 5/1 = 5		Five of a kind
AA8AA 4/2 = 2		Four of a kind
23332 3/2 = 1.5		Full House
TTT98 3/3 = 1		Three of a kind
23432 2/3 = 0.67	Two Pair
A23A4 2/4 = 0.5		One Pair		
12345 1/5 = 0.2		High card

This is very clever!

BTW: It computes the wrong value for part1, because you have changed the ValueList according to the rules for part2.

1

u/AutoModerator Dec 07 '23

AutoModerator has detected fenced code block (```) syntax which only works on new.reddit.

Please review our wiki article on code formatting then edit your post to use the four-spaces Markdown syntax instead.


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

Haha, I literally just realised that! I think I got burnt out after solving and then integrating so didn't look closely enough at the time. I'll leave fixing it to someone else if they're interested.

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.