Day 2

AdventOfCode > 2022

Part 1: What would your total score be if everything goes exactly according to your strategy guide?

I manually downloaded my personal day 2 input file as a logged user, and here I get the data.

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0      ✔ purrr   0.3.5 
✔ tibble  3.1.8      ✔ dplyr   1.0.10
✔ tidyr   1.2.1      ✔ stringr 1.4.1 
✔ readr   2.1.3      ✔ forcats 0.5.2 
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
data <- 
    read_delim(
        here::here("2022/02_input"),
        col_names = c("opponent", "me"),
        show_col_types = FALSE,
        delim=" "
    )
data %>% print(n = 30)
# A tibble: 2,500 × 2
   opponent me   
   <chr>    <chr>
 1 B        Y    
 2 A        X    
 3 B        Y    
 4 A        Y    
 5 A        Z    
 6 B        Y    
 7 B        Z    
 8 C        Y    
 9 A        X    
10 C        X    
11 A        X    
12 B        Y    
13 C        Y    
14 B        Y    
15 B        Z    
16 C        X    
17 C        Z    
18 A        X    
19 B        Y    
20 C        Z    
21 C        Y    
22 B        X    
23 B        Y    
24 A        X    
25 B        Z    
26 B        Z    
27 B        Y    
28 C        Z    
29 B        X    
30 B        Z    
# … with 2,470 more rows

I define the rules to play and I codify the data according to my entities representation:

entities <- c(
    A = "rock", B = "paper", C = "scissors",
    X = "rock", Y = "paper", Z = "scissors"
)
data %>% 
    mutate(opponent = entities[opponent]) %>% 
    mutate(me = entities[me])
# A tibble: 2,500 × 2
   opponent me      
   <chr>    <chr>   
 1 paper    paper   
 2 rock     rock    
 3 paper    paper   
 4 rock     paper   
 5 rock     scissors
 6 paper    paper   
 7 paper    scissors
 8 scissors paper   
 9 rock     rock    
10 scissors rock    
# … with 2,490 more rows

I add a column for each turn result according to the game rules:

lost <- 0
draw <- 3
win <- 6
rules <- tribble(
    ~opponent,  ~me,         ~my_outcome,
    "rock",     "scissors",  lost,                  
    "scissors", "paper",     lost,
    "paper",    "rock",      lost,
    "rock",     "rock",      draw,
    "scissors", "scissors",  draw,
    "paper",    "paper",     draw,
    "scissors", "rock",      win,
    "paper",    "scissors",  win,
    "rock",     "paper",     win,
)
data %>% 
    mutate(opponent = entities[opponent]) %>% 
    mutate(me = entities[me])
# A tibble: 2,500 × 2
   opponent me      
   <chr>    <chr>   
 1 paper    paper   
 2 rock     rock    
 3 paper    paper   
 4 rock     paper   
 5 rock     scissors
 6 paper    paper   
 7 paper    scissors
 8 scissors paper   
 9 rock     rock    
10 scissors rock    
# … with 2,490 more rows

I get the score for each turn:

lose <- 0
draw <- 3
win <- 6

picked_score <- c(
  "rock" = 1,
  "paper" = 2, 
  "scissors" = 3
)
rules <- tribble(
    ~opponent,  ~me,         ~my_outcome,
    "rock",     "scissors",  lose,                  
    "scissors", "paper",     lose,
    "paper",    "rock",      lose,
    "rock",     "rock",      draw,
    "scissors", "scissors",  draw,
    "paper",    "paper",     draw,
    "scissors", "rock",      win,
    "paper",    "scissors",  win,
    "rock",     "paper",     win,
)
turns <- data %>% 
    mutate(opponent = entities[opponent]) %>% 
    mutate(me = entities[me]) %>% 
    mutate(picked_score = picked_score[me]) %>% 
    left_join(rules, by = c("opponent", "me")) %>% 
    mutate(score = picked_score + my_outcome) 

turns
# A tibble: 2,500 × 5
   opponent me       picked_score my_outcome score
   <chr>    <chr>           <dbl>      <dbl> <dbl>
 1 paper    paper               2          3     5
 2 rock     rock                1          3     4
 3 paper    paper               2          3     5
 4 rock     paper               2          6     8
 5 rock     scissors            3          0     3
 6 paper    paper               2          3     5
 7 paper    scissors            3          6     9
 8 scissors paper               2          0     2
 9 rock     rock                1          3     4
10 scissors rock                1          6     7
# … with 2,490 more rows

And the total score:

sum(turns$score)
[1] 12740

Part 2: What would your total score be if everything goes exactly according to your strategy guide?

I codify entites to get my turn score.

entities <- c(
    A = "rock", B = "paper", C = "scissors",
    X =  lose, Y = draw, Z = win
)
data %>% 
    mutate(opponent = entities[opponent]) %>% 
    mutate(me = entities[me])
# A tibble: 2,500 × 2
   opponent me   
   <chr>    <chr>
 1 paper    3    
 2 rock     0    
 3 paper    3    
 4 rock     3    
 5 rock     6    
 6 paper    3    
 7 paper    6    
 8 scissors 3    
 9 rock     0    
10 scissors 0    
# … with 2,490 more rows

I get my strategy table.

picked_score <- c(
  "rock" = 1,
  "paper" = 2, 
  "scissors" = 3
)
strategy <- rules %>% 
    mutate(picked_score = picked_score[me]) %>% 
    mutate(score = my_outcome + picked_score) %>% 
    transmute(opponent, me = my_outcome, score)

strategy
# A tibble: 9 × 3
  opponent    me score
  <chr>    <dbl> <dbl>
1 rock         0     3
2 scissors     0     2
3 paper        0     1
4 rock         3     4
5 scissors     3     6
6 paper        3     5
7 scissors     6     7
8 paper        6     9
9 rock         6     8
turns <- data %>% 
    mutate(opponent = entities[opponent]) %>% 
    mutate(me = as.integer(entities[me])) %>% 
    left_join(strategy, by = c("opponent", "me"))

turns
# A tibble: 2,500 × 3
   opponent    me score
   <chr>    <dbl> <dbl>
 1 paper        3     5
 2 rock         0     3
 3 paper        3     5
 4 rock         3     4
 5 rock         6     8
 6 paper        3     5
 7 paper        6     9
 8 scissors     3     6
 9 rock         0     3
10 scissors     0     2
# … with 2,490 more rows

And the total score:

sum(turns$score)
[1] 11980