Advent of Code: 2022 Day 2 in R

r
advent of code
puzzle
Published

September 7, 2023

Below is my solution for Advent of Code 2022 day 2 in R. See the puzzle instructions here.

Part 1

Read Input

As always, the first step is to read the input. I have saved the example data in test_input.txt and the real puzzle input in input.txt. I know that the input has two letters per line separated by a space, which is exactly what read.delim() is made for. I’ll specify header = FALSE and sep = " ", and keep the default variable names V1 and V2.

read_input <- function(file) {
  read.delim(file, header = FALSE, sep = " ")
}

Just to see what the test data looks like:

test_data <- read_input("test_input.txt")
test_data
  V1 V2
1  A  Y
2  B  X
3  C  Z

Scoring Function

We need to create a function that will determine our total score based on what shape we played and what the outcome was. Remember for the first column, A is Rock, B is Paper, and C is Scissors. For the second column, X is Rock, Y is Paper, and Z is scissors. For the outcomes, you get 0 points for losing, 3 for drawing, and 6 for winning.

First, we have to determine the outcome. Unfortunately, this is somewhat tedious but the most straightforward way is to code it by hand.

find_outcome <- function(p1, p2) {
  if (p1 == "A") {
    if (p2 == "X") return(3) # Rock-Rock=Draw
    if (p2 == "Y") return(6) # Rock-Paper=Win
    if (p2 == "Z") return(0) # Rock-Scissors=Lose
  }
  if (p1 == "B") {
    if (p2 == "X") return(0) # Paper-Rock=Lose
    if (p2 == "Y") return(3) # Paper-Paper=Draw
    if (p2 == "Z") return(6) # Paper-Scissors=Win
  }
  if (p1 == "C") {
    if (p2 == "X") return(6) # Scissors-Rock=Win
    if (p2 == "Y") return(0) # Scissors-Paper=Lose
    if (p2 == "Z") return(3) # Scissors-Scissors=Draw
  }
}

We can create a simple vector for points based on shape selection. You get 1 point for Rock ("X"), 2 points for Paper ("Y"), and 3 points for Scissors ("Z"). So I can make a vector of those shapes and pull out the scores using which().

find_shape_score <- function(shape) {
  shape_scores <- c("X", "Y", "Z")
  which(shape_scores == shape)
}

Finally, we need to combine those to get the total score.

find_total_score <- function(p1, p2) {
  outcome <- find_outcome(p1, p2)
  shape_score <- find_shape_score(p2)
  outcome + shape_score
}

Let’s apply that to the example data. I will add a column to the test_data data frame with the expected results from the example:

test_data$expected <- c(8, 1, 6)

Then we can add a column with the calculated scores. Because I haven’t written the functions to work with vectors of inputs, I iterate over the data frame.

for (i in seq_len(nrow(test_data))) {
  test_data$calculated[i] <- find_total_score(test_data$V1[i], test_data$V2[i])
}
test_data
  V1 V2 expected calculated
1  A  Y        8          8
2  B  X        1          1
3  C  Z        6          6

And in code, we can see:

all(test_data$expected == test_data$calculated)
[1] TRUE

I’ll just put that into a function to re-use for the real data:

calculate_all_scores <- function(input_data) {
  calculated <- numeric(nrow(input_data))
  for (i in seq_len(nrow(input_data))) {
    calculated[i] <- find_total_score(input_data$V1[i], input_data$V2[i])
  }
  calculated
}

So the last part is getting the sum:

part_1 <- function(input_data) {
  calculated <- calculate_all_scores(input_data)
  sum(calculated)
}

Try it with the test data:

part_1(test_data)
[1] 15

Which is what we expect.

Now we try it on the real data:

real_data <- read_input("input.txt")
part_1(real_data)
[1] 12855

And that’s correct!

Part 2

So now, instead of finding the outcome from each player’s shape, we need to find the right shape to play based on the outcome. "X" means lose, "Y" means draw, and "Z" means win.

find_correct_shape <- function(p1, outcome) {
  if (p1 == "A") {
    if (outcome == "X") return("scissors") # Rock+Lose=Scissors
    if (outcome == "Y") return("rock")     # Rock+Draw=Rock
    if (outcome == "Z") return("paper")    # Rock+Win=Paper
  }
  if (p1 == "B") {
    if (outcome == "X") return("rock")     # Paper+Lose=Rock
    if (outcome == "Y") return("paper")    # Paper+Draw=Paper
    if (outcome == "Z") return("scissors") # Paper+Win=Scissors
  }
  if (p1 == "C") {
    if (outcome == "X") return("paper")    # Scissors+Lose=Paper
    if (outcome == "Y") return("scissors") # Scissors+Draw=Scissors
    if (outcome == "Z") return("rock")     # Scissors+Win=Rock
  }
}

I’ll make a new find_shape_score() function using the actual shape names:

find_shape_score <- function(shape) {
  shape_scores <- c("rock", "paper", "scissors")
  which(shape_scores == shape)
}

I also need to make a lookup for outcome points. I could write this into the bigger functions but I’ll make it its own just to be super clear about what’s going on.

find_outcome_score <- function(outcome) {
  if (outcome == "X") return(0)
  if (outcome == "Y") return(3)
  if (outcome == "Z") return(6)
}

I need to make a new find_total_score() function as well:

find_total_score <- function(p1, outcome) {
  correct_shape <- find_correct_shape(p1, outcome)
  shape_score <- find_shape_score(correct_shape)
  outcome_score <- find_outcome_score(outcome)
  shape_score + outcome_score
}

I’ll test this out again on the test data. I read it in one more time just to get a clean slate since I added some extra columns in part 1. I can still use the same calculate_all_scores() function because I kept the same names for the intermediate functions, and that function just applies find_total_score() to the rows of the data frame.

test_data <- read_input("test_input.txt")
test_data$expected <- c(4, 1, 7)
test_data$calculated <- calculate_all_scores(test_data)
test_data
  V1 V2 expected calculated
1  A  Y        4          4
2  B  X        1          1
3  C  Z        7          7

Looks good! Now we just come up with the total:

sum(test_data$calculated)
[1] 12

Which matches the example. Now for the real data:

part_2 <- function(input_data) {
  calculated <- calculate_all_scores(input_data)
  sum(calculated)
}
part_2(real_data)
[1] 13726

Which is right! There we go.