2022-14: Extended Polymerization

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(stringr)
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 <- readLines("input.txt")
polymer <- dt[!grepl(" -> ", dt)][1]
polymer <- str_split(polymer, "", simplify = TRUE)[1,]
pairs <- 
  data.frame(str_split(dt[grepl(" -> ", dt)], " -> ", simplify = TRUE))

Part 1

dt <- readLines("input.txt")
polymer <- dt[!grepl(" -> ", dt)][1]
polymer <- str_split(polymer, "", simplify = TRUE)[1,]
pairs <- data.frame(str_split(dt[grepl(" -> ", dt)], " -> ", simplify = TRUE))

for(d in 1:10) {
  pos <- data.frame("let" = NA, "pos" = NA)
  for (i in 1:(length(polymer) - 1)) {
    pos <- rbind(pos, data.frame("let" =
                                   pairs$X2[pairs$X1 == paste0(polymer[i:(i + 1)],
                                                               collapse = "")],
                                 "pos" = i))
  }
  pos <- pos[-1, ]
  
  for (r in 1:nrow(pos)) {
    polymer <-
      R.utils::insert(x = polymer,
                      values = pos$let[r],
                      ats = r + pos$pos[r])
  }
  # print((table(polymer)))
}
t <- sort(table(polymer), decreasing = TRUE)
t[1] - rev(t)[1]
   S 
3230 

Part 2

dt <- readLines("input.txt")
polymer <- dt[!grepl(" -> ", dt)][1]
polymer <- str_split(polymer, "", simplify = TRUE)[1,]
pairs <- 
  data.frame(str_split(dt[grepl(" -> ", dt)], " -> ", simplify = TRUE))

dt <-
  data.frame("x" = polymer[-length(polymer)], "y" = polymer[-1]) %>%
  mutate("z" = paste0(x, y, sep = "")) %>%
  select("z") %>%
  group_by(z) %>%
  count()
  
counts <- unique(unlist(strsplit(pairs$X1, "")))
counts <- 
  rep(0, length(counts)) %>%
  `names<-`(sort(unique(counts))) %>%
  data.frame() %>%
  rownames_to_column(var = "value") %>%
  `colnames<-`(c("value", "n")) %>%
  rbind(table(polymer) |> data.frame() |> `colnames<-`(c("value", "n"))) %>%
  group_by(value) |>
    summarise(n = sum(n)) |>
    ungroup()

build_poly <- function(dt, counts, i = 1) {
  if (i == 40 + 1) { return(counts) }
  dt <- dt %>%
    right_join(pairs, by = c("z" = "X1")) %>%
    filter(!is.na(n))
  
  counts <-
    dt %>%
    group_by(X2) %>%
      summarise(n = sum(n)) %>%
    ungroup() %>%
    `colnames<-`(c("value", "n")) %>%
    rbind(counts) %>%
    group_by(value) %>%
      summarise(n = sum(n)) %>%
    ungroup() %>%
    filter(!is.na(n))
  
  dt <- dt %>%
    separate("z", into = c("x", "y"), sep = 1) %>%
    mutate(x = paste(x, X2, sep = ""),
           y = paste(X2, y, sep = "")) %>%
    select(-X2) %>%
    pivot_longer(cols = c(x, y)) %>%
    select(-name) %>%
    group_by(value) %>%
      summarise(n = sum(n)) %>%
    ungroup() %>%
    rename("z" = value)
  
  build_poly(dt, counts, i + 1)
}

options(digits = 20)

tab <- 
  build_poly(dt, counts) |>
  deframe() |>
  sort()

tab[length(tab)] - tab[1]
            S 
3542388214529