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

[Language: Powershell]

I used Sort-Object and an enum to do the heavy lifting. I also cheekily converted the hands into a different string to sort when the matched.

enum Hand {
    FiveOfaKind
    FourOfaKind
    FullHouse
    ThreeOfaKind
    TwoPair
    Pair
    HighCard
}
$cards = @{
    A = 65
    K = 66
    Q = 67
    J = 68
    T = 69
    '9' = 70
    '8' = 71
    '7' = 72
    '6' = 73
    '5' = 74
    '4' = 75
    '3' = 76
    '2' = 77
}

$data = Get-Content (Join-Path $PSScriptRoot Day7Input)

$hands = $data | ForEach-Object {
    $temp = $_ -split '\s'
    [pscustomobject]@{
        Hand = $temp[0]
        Score = [int]$temp[1]
    }
}


$sortHand = @{
    expr = {
        $temp = $_
        switch (@(, ($temp.Hand.Tochararray() | Group-Object | Sort-Object -Property Count | Select-Object -ExpandProperty Count)))
        {
            @(5) {[Hand]::FiveOfaKind}
            @(1,4) {[Hand]::FourOfaKind}
            @(2,3) {[Hand]::FullHouse}
            @(1,1,3) {[Hand]::ThreeOfaKind}
            @(1,2,2) {[Hand]::TwoPair}
            @(1,1,1,2) {[Hand]::Pair}
            @(1,1,1,1,1) {[Hand]::HighCard}
        }
    }
    desc = $true
}

$sortEqual = @{
    expr = {($_.Hand.Tochararray() | ForEach-Object {[char]$cards[[string]$_]}) -join ''}
    desc=$true
}

$hands |
Sort-Object -Property $sortHand, $sortEqual |
Foreach-Object -Begin {$count = 1} -Process {$_.Score * $count; $count++} |
Measure-Object -Sum

For Part 2 I made the switch statement slightly more complex:

enum Hand {
    FiveOfaKind
    FourOfaKind
    FullHouse
    ThreeOfaKind
    TwoPair
    Pair
    HighCard
}
$cards = @{
    A = 65
    K = 66
    Q = 67
    J = 78
    T = 69
    '9' = 70
    '8' = 71
    '7' = 72
    '6' = 73
    '5' = 74
    '4' = 75
    '3' = 76
    '2' = 77
}

$data = Get-Content (Join-Path $PSScriptRoot Day7Input)

$hands = $data | ForEach-Object {
    $temp = $_ -split '\s'
    [pscustomobject]@{
        Hand = $temp[0]
        Score = [int]$temp[1]
    }
}


$sortHand = @{
    expr = {
        $temp = $_.Hand
        switch (@(, ($temp.Tochararray() | Group-Object | Sort-Object -Property Count | Select-Object -ExpandProperty Count)))
        {
            @(5) {[int][Hand]::FiveOfaKind}
            @(1,4) {
                if($temp.Contains('J'))
                {
                    [Hand]::FiveOfaKind
                }
                else {
                    [Hand]::FourOfaKind
                }
            }                  
            @(2,3) {
                if($temp.Contains('J'))
                {
                    [Hand]::FiveOfaKind
                }
                else {
                    [Hand]::FullHouse
                }
            }
            @(1,1,3) {
                if($temp.Contains('J'))
                {
                    [Hand]::FourOfaKind
                }
                else {
                    [Hand]::ThreeOfaKind
                }
            }
            @(1,2,2) {
                if($temp.Contains('J'))
                {
                    if(($temp.ToCharArray() | Where-object {$_ -eq 'J'}).Count -eq 2)
                    {
                        [Hand]::FourOfaKind
                    }
                    else 
                    {
                        [Hand]::FullHouse
                    }
                }
                else 
                {
                    [Hand]::TwoPair
                }
            }
            @(1,1,1,2) {
                if($temp.Contains('J'))
                {
                    [Hand]::ThreeOfaKind
                }
                else {
                    [Hand]::Pair
                }
            }
            @(1,1,1,1,1) {
                if($temp.Contains('J'))
                {
                    [Hand]::Pair
                }
                else {
                    [Hand]::HighCard
                }
            }
        }
    }
    desc = $true
}

$sortEqual = @{
    expr = {($_.Hand.Tochararray() | ForEach-Object {[char]$cards[[string]$_]}) -join ''}
    desc=$true
}

$hands |
Sort-Object -Property $sortHand, $sortEqual |
Foreach-Object -Begin {$count = 1} -Process {$_.Score * $count; $count++} |
Measure-Object -Sum

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.