crlogic

Day2

Dec 4th, 2022
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. <#
  2. "The first column is what your opponent is going to play: A for Rock, B for Paper, and C for Scissors. The second column--"
  3. X for Rock, Y for Paper, and Z for Scissors
  4. total score is the sum of your scores for each round
  5. score for a single round is the score for the shape you selected (1 for Rock, 2 for Paper, and 3 for Scissors),
  6. 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)
  7. #>
  8. $gameRounds = $puzzelInput -split '\n' | Select -SkipLast 1
  9. Function Invoke-PaperRockScissors {
  10. Param ([string[]]$gameRounds)
  11. Process {
  12. $didIlose = @{
  13. 'A Z' = $true
  14. 'B X' = $true
  15. 'C Y' = $true
  16. }
  17. $translateMove = @{
  18. 'X' = 'A'
  19. 'Y' = 'B'
  20. 'Z' = 'C'
  21. }
  22. $shapeScore = @{
  23. 'X' = 1
  24. 'Y' = 2
  25. 'Z' = 3
  26. }
  27. $roundScores = foreach ($row in $gameRounds) {
  28. $opponentMove,$playerMove = $null
  29. $opponentMove,$playerMove = $row -split ' '
  30.  
  31. # Did I lose?
  32. If ($didIlose["$row"] -eq $true) {
  33. $shapeScore["$playerMove"]
  34. continue
  35. }
  36.  
  37. # Did I tie?
  38. If ($opponentMove -eq $translateMove["$playerMove"]) {
  39. $shapeScore["$playerMove"] + 3
  40. continue
  41. }
  42.  
  43. $shapeScore["$playerMove"] + 6
  44. }
  45. ($roundScores | Measure-Object -Sum).Sum
  46. }
  47. }
  48. Invoke-PaperRockScissors $gameRounds
  49.  
  50. # Part 2
  51. # X means you need to lose, Y means you need to end the round in a draw, and Z means you need to win
  52. $translateStrategy = @{
  53. 'AL' = 'A Z'
  54. 'AD' = 'A X'
  55. 'AW' = 'A Y'
  56. 'BL' = 'B X'
  57. 'BD' = 'B Y'
  58. 'BW' = 'B Z'
  59. 'CL' = 'C Y'
  60. 'CD' = 'C Z'
  61. 'CW' = 'C X'
  62. }
  63. $newPuzzelInput = foreach ($row in $gameRounds) {
  64. $opponentMove,$playerMove = $null
  65. $opponentMove,$playerMove = $row -split ' '
  66. switch ($playerMove) {
  67. 'X' {$translateStrategy["$($opponentMove)L"]} # lose
  68. 'Y' {$translateStrategy["$($opponentMove)D"]} # draw
  69. 'Z' {$translateStrategy["$($opponentMove)W"]} #
  70. }
  71. }
  72. Invoke-PaperRockScissors $newPuzzelInput
Advertisement
Add Comment
Please, Sign In to add comment