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!

105 Upvotes

1.5k comments sorted by

View all comments

2

u/idontknowwhattouse33 Dec 02 '22

Powershell. Lots of hashtables..

<#
"The first column is what your opponent is going to play: A for Rock, B for Paper, and C for Scissors. The second column--"
X for Rock, Y for Paper, and Z for Scissors
total score is the sum of your scores for each round
score for a single round is the score for the shape you selected (1 for Rock, 2 for Paper, and 3 for Scissors),
plus the score for the outcome of the round (0 if you lost, 3 if the round was a draw, and 6 if you won)
#>
$gameRounds = $puzzelInput -split '\n' | Select -SkipLast 1
Function Invoke-PaperRockScissors {
    Param ([string[]]$gameRounds)
    Process {
        $didIlose = @{
            'A Z' = $true
            'B X' = $true
            'C Y' = $true
        }
        $translateMove = @{
            'X' = 'A'
            'Y' = 'B'
            'Z' = 'C'
        }
        $shapeScore = @{
            'X' = 1
            'Y' = 2
            'Z' = 3
        }
        $roundScores = foreach ($row in $gameRounds) {
            $opponentMove,$playerMove = $null
            $opponentMove,$playerMove = $row -split ' '

            # Did I lose?
            If ($didIlose["$row"] -eq $true) {
                $shapeScore["$playerMove"]
                continue
            }

            # Did I tie?
            If ($opponentMove -eq $translateMove["$playerMove"]) {
                $shapeScore["$playerMove"] + 3
                continue
            }
            
            $shapeScore["$playerMove"] + 6
        }
        ($roundScores | Measure-Object -Sum).Sum
    }
}
Invoke-PaperRockScissors $gameRounds

# Part 2
# X means you need to lose, Y means you need to end the round in a draw, and Z means you need to win
$translateStrategy = @{
    'AL' = 'A Z'
    'AD' = 'A X'
    'AW' = 'A Y'
    'BL' = 'B X'
    'BD' = 'B Y'
    'BW' = 'B Z'
    'CL' = 'C Y'
    'CD' = 'C Z'
    'CW' = 'C X'
}
$newPuzzelInput = foreach ($row in $gameRounds) {
    $opponentMove,$playerMove = $null
    $opponentMove,$playerMove = $row -split ' '
    switch ($playerMove) {
        'X' {$translateStrategy["$($opponentMove)L"]} # lose
        'Y' {$translateStrategy["$($opponentMove)D"]} # draw
        'Z' {$translateStrategy["$($opponentMove)W"]} # 
    }
}
Invoke-PaperRockScissors $newPuzzelInput

1

u/daggerdragon Dec 05 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.