# Advent of Code 2022, Day 2: Rock Paper Scissors

## Part A

Day 2 is the game Rock Paper Scissors. We have an input file with a strategy guide:

``````A Y
B X
C Z
``````

The first column is your opponent move and in the second how you should play. A is for Rock, B for Paper and C for Scissors. And the same X for Rock, Y for Paper and Z for Scissors. For me it is a bit confusing to reference those values in the code, so I remapped them during reading from input file:

``````moves = { "A" => "R", "B" => "P", "C" => "S", "X" => "R", "Y" => "P", "Z" => "S" }

data = File.
map(&:strip).
map { |item| item.split(" ") }.map do |(opponent, me)|
[moves[opponent], moves[me]]
end
``````

What we need to do is calculate the final score. Here are the rules:

• In the first round, your opponent will choose Rock (A), and you should choose Paper (Y). This ends in a win for you with a score of 8 (2 because you chose Paper + 6 because you won).
• In the second round, your opponent will choose Paper (B), and you should choose Rock (X). This ends in a loss for you with a score of 1 (1 + 0).
• The third round is a draw with both players choosing Scissors, giving you a score of 3 + 3 = 6.

In this example, if you were to follow the strategy guide, you would get a total score of 15 (8 + 1 + 6).

``````moves = { "A" => "R", "B" => "P", "C" => "S", "X" => "R", "Y" => "P", "Z" => "S" }

data = File.
map(&:strip).
map { |item| item.split(" ") }.map do |(opponent, me)|
[moves[opponent], moves[me]]
end

map = {
"R" => 1,
"P" => 2,
"S" => 3
}

results = data.map do |(opponent, me)|
points = 0

if (opponent == "R" && me == "P") || (opponent == "P" && me == "S") || (opponent == "S" && me == "R")
points += 6
elsif opponent == me
points += 3
end

points += map[me]

points
end

puts results.sum
``````

## Part B

In Part B rules change to:

• In the first round, your opponent will choose Rock (A), and you need the round to end in a draw (Y), so you also choose Rock. This gives you a score of 1 + 3 = 4.
• In the second round, your opponent will choose Paper (B), and you choose Rock so you lose (X) with a score of 1 + 0 = 1.
• In the third round, you will defeat your opponent’s Scissors with Rock for a score of 1 + 6 = 7.

Now for the initial example you would get a total score of 12.

I am lazy so I just mapped all possible inputs, their respective scores and summed them up together:

``````moves = { "A" => "R", "B" => "P", "C" => "S", "X" => "R", "Y" => "P", "Z" => "S" }

strategy = {
# Rock
"AX" => 0 + 3,
"AY" => 3 + 1,
"AZ" => 6 + 2,
# Paper
"BX" => 0 + 1,
"BY" => 3 + 2,
"BZ" => 6 + 3,
# Scissors
"CX" => 0 + 2,
"CY" => 3 + 3,
"CZ" => 6 + 1
}

results = File.
Just to explain. For example `"CZ" => 6 + 1`, opponent is playing Scissors and we need to win. We will get 6 points for the win and 1 point for playing Rock.