2022-03: Rucksack Reorganization

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 already looks rough. readLines because it’s strings.

Part 1

Ultimately not too bad. I got to make use of \(x) for an anonymous function. I messed up the second half of the string and had it started at the end of the first half. I got docked a minute for the bad answer but then was able to fix it in time for resubmission.

data.frame("input" = dt) %>%
  mutate(V1 = substr(input, 1, nchar(input) / 2),
         V2 = substr(input, nchar(input) / 2 + 1, nchar(input))) %>%
  apply(1, \(x) {
    v1 <- unlist(strsplit(x["V1"], ""))
    v2 <- unlist(strsplit(x["V2"], ""))
    
    return(v1[v1 %in% v2])
  }) %>%
  sapply(\(x) {
    x <- unique(x)
    sum(which(letters == x), (which(LETTERS == x) + 26), na.rm = TRUE)
  }) %>%
  sum()
[1] 7811

Part 2

I struggled with creating the groups for some reason. Once I had that going, the rest was pretty easy.

data.frame("input" = dt) %>%
  mutate("group" = rep(1:(nrow(.) / 3), each = 3)) -> dt2

d2 <- c()
for(g in unique(dt2$group)) {
  d <- dt2$input[dt2$group == g]
  
  v1 <- unlist(strsplit(d[1], ""))
  v2 <- unlist(strsplit(d[2], ""))
  v3 <- unlist(strsplit(d[3], ""))
    
  d2 <<- c(d2, unique(v3[v3 %in% v1[v1 %in% v2]]))
}

sapply(d2, \(x) {
    x <- unique(x)
    sum(which(letters == x), (which(LETTERS == x) + 26), na.rm = TRUE)
  }) %>%
  sum()
[1] 2639