2022-02: Rock Paper Scissors

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()
library(data.table)

Attaching package: 'data.table'

The following objects are masked from 'package:dplyr':

    between, first, last

The following object is masked from 'package:purrr':

    transpose
dt <- fread("input.txt", header = FALSE)
# dt <- readLines("input.txt")

This isn’t so bad, just rock, paper, scissors.

Part 1

I initially did V1 == V2 but that doesn’t work with ABC and XYZ, but a quick fix put me at 549 on the leaderboard so I’m really pleased.

dt %>%
  mutate("my_score" = case_when(
    V2 == "X" ~ 1,
    V2 == "Y" ~ 2,
    V2 == "Z" ~ 3
  )) %>%
  mutate("win_score" = case_when(
    (V1 == "A" & V2 == "X") | (V1 == "B" & V2 == "Y") | (V1 == "C" & V2 == "Z") ~ 3,
    V1 == "A" & V2 == "Y" ~ 6,
    V1 == "A" & V2 == "Z" ~ 0,
    V1 == "B" & V2 == "X" ~ 0,
    V1 == "B" & V2 == "Z" ~ 6,
    V1 == "C" & V2 == "X" ~ 6,
    V1 == "C" & V2 == "Y" ~ 0
  )) %>%
  mutate("score" = my_score + win_score) %>%
  pull("score") %>%
  sum()
[1] 14163

Part 2

I had this one pretty quick, just a bit of logic to get the play column right. Of course, I then had to remember to change all the right V2 references which took me a minute to figure out when I missed the second mutate.

dt %>%
  mutate("play" = case_when(
    V2 == "X" & V1 == "A" ~ "Z",
    V2 == "X" & V1 == "B" ~ "X",
    V2 == "X" & V1 == "C" ~ "Y",
    V2 == "Y" & V1 == "A" ~ "X",
    V2 == "Y" & V1 == "B" ~ "Y",
    V2 == "Y" & V1 == "C" ~ "Z",
    V2 == "Z" & V1 == "A" ~ "Y",
    V2 == "Z" & V1 == "B" ~ "Z",
    V2 == "Z" & V1 == "C" ~ "X",
  )) %>%
  mutate("my_score" = case_when(
    play == "X" ~ 1,
    play == "Y" ~ 2,
    play == "Z" ~ 3
  )) %>%
  mutate("win_score" = case_when(
    (V1 == "A" & play == "X") | (V1 == "B" & play == "Y") | (V1 == "C" & play == "Z") ~ 3,
    V1 == "A" & play == "Y" ~ 6,
    V1 == "A" & play == "Z" ~ 0,
    V1 == "B" & play == "X" ~ 0,
    V1 == "B" & play == "Z" ~ 6,
    V1 == "C" & play == "X" ~ 6,
    V1 == "C" & play == "Y" ~ 0
  )) %>%
  mutate("score" = my_score + win_score) %>%
  pull("score") %>%
  sum()
[1] 12091