r/adventofcode • • Dec 02 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 2 Solutions -🎄-

NEW AND NOTEWORTHY


--- Day 2: Rock Paper Scissors ---


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:06:16, megathread unlocked!

103 Upvotes

1.5k comments sorted by

View all comments

2

u/CBRSi76 Dec 02 '22 edited Dec 05 '22

Learning Powershell, so I'm sure this is a hot mess. Helpful comments for tidying/improving my ramblings greatly appreciated 😊

Part 1...

## Advent of Code 2022 - Day 2 - Part 1

# Get input data
$guide = Get-Content .\input.txt

$myTotalScore = 0

# Get value of a play... Rock: 1, Paper: 2, Scissors: 3 
function Get-Value {
    param (
        [Parameter(Mandatory)]  [String]$RPS
    )
    if( $RPS -eq "X" -or $RPS -eq "A" ) { return 1 }
    if ($RPS -eq "Y" -or $RPS -eq "B" ) { return 2 }
    if ($RPS -eq "Z" -or $RPS -eq "C" ) { return 3 }
}

function WinLoseDraw {
    param (
        [String]$me,
        [String]$opp
    )
    switch ($me) {
        "X" { 
            if ($opp -eq  "C") { return 6} 
            elseif ($opp -eq "A") { return 3}
            else { return 0 }
        }
        "Y" {
            if ($opp -eq  "A") { return 6} 
            elseif ($opp -eq "B") { return 3}
            else { return 0 }
        }
        "Z" {
            if ($opp -eq  "B") { return 6} 
            elseif ($opp -eq "C") { return 3}
            else { return 0 }
        }
        Default {}
    }
} 


# Go thru all games
foreach ( $game in $guide ){

    # Separate mine and opponents plays
    $opponentPlays, $iPlay = $game.Split(" ")

    $myScore  = Get-Value($iPlay)
    $myTotalScore += $myScore

    # Add 6 points for a win, 3 for a draw
    $myTotalScore += WinLoseDraw $iPlay $opponentPlays
}

$myTotalScore

And my part2...

## Advent of Code 2022 - Day 2 - Part 2

# Get input data
$guide = Get-Content .\input.txt
$myTotalScore = 0

# Get value of a play... Rock: 1, Paper: 2, Scissors: 3 
function Get-Value {
    param (
        [Parameter(Mandatory)]  [String]$RPS
    )
    switch ($RPS) {
        "A" { return 1 }
        "B" { return 2 }
        "C" { return 3 }
        Default {}
    }
}

function WinLoseDraw {
    param (
        [String]$me,
        [String]$opp
    )
    switch ($opp) {
        "A" { 
            if ($me -eq  "B") { return 6} # Win
            elseif ($me -eq "A") { return 3} # Draw
            else { return 0 } # Lose
        }
        "B" {
            if ($me -eq  "C") { return 6} 
            elseif ($me -eq "B") { return 3}
            else { return 0 }
        }
        "C" {
            if ($me -eq  "A") { return 6} 
            elseif ($me -eq "C") { return 3}
            else { return 0 }
        }
        Default {}
    }

} 

function GetNextPlay {
    param (
        [String]$opp,
        [String]$desiredResult
    )
    switch ($opp) {
        "A" { 
            if ($desiredResult -eq  "X") { return "C"} # Lose
            elseif ($desiredResult -eq "Y") { return "A"} # Draw
            else { return "B" } # Win
        }
        "B" {
            if ($desiredResult -eq  "X") { return "A"}  # Lose
            elseif ($desiredResult -eq "Y") { return "B"} # Draw
            else { return "C" } # Win
        }
        "C" {
            if ($desiredResult -eq  "X") { return "B"}  # Lose
            elseif ($desiredResult -eq "Y") { return "C"} # Draw
            else { return "A" } # Win
        }
        Default {}
    }
}

# Go thru all games
foreach ( $game in $guide ){

    # Separate mine and opponents plays
    $opponentPlays, $iPlay = $game.Split(" ")
    $myNextPlay = GetNextPlay $opponentPlays $iPlay

    $myScore  = Get-Value($myNextPlay)
    $myTotalScore += $myScore

    # Add 6 points for a win, 3 for a draw
    $myTotalScore += WinLoseDraw $myNextPlay $opponentPlays
}

$myTotalScore

1

u/daggerdragon Dec 05 '22 edited Dec 05 '22

Same as your part 2... and put both in the same thread, please.

Edit: thanks for fixing your code block.

2

u/CBRSi76 Dec 05 '22

Thanks for the guidance, I think I've formatted it correctly now, could you please confirm? Many thanks