Trash Compactor

Published

December 6, 2025

library(mistlecode)

options(scipen = 999)

Part 1

Used apply because it’s much better at this sort of thing than purrr::pmap().

'input.txt' |>
  readLines() |>
  stringr::str_split('\\s+') |>
  purrr::map(\(x) x[x != '']) |>
  do.call(rbind, args = _) |>
  apply(\(x) {
    x[-length(x)] |>
      paste0(collapse = x[length(x)]) |>
      parse(text = _) |>
      eval()
  }, MARGIN = 2) |>
  sum()
[1] 4405895212738

Part 2

Took me way too long to realize my problems where because numbers could be left or right padded and then I had to figure out how to deal with it.

which_op <-
  'input.txt' |> 
  readLines() |>
  utils::tail(1) |>
  stringr::str_split_1('') |>
  (\(x) which(x != ' '))()

'input.txt' |>
  readLines() |>
  # utils::head(-1) |>
  purrr::map(\(x) {
    x |>
      stringr::str_sub_all(start = which_op, end = c(which_op[-1] -1, nchar(x))) |>
      unlist()
  }) |>
  do.call(rbind, args = _) |>
  apply(\(x) {
    x[-length(x)] |>
      stringr::str_split('') |>
      do.call(rbind, args = _) |>
      apply(paste0, MARGIN = 2, collapse = '') |>
      stringr::str_trim() |>
      (\(x) x[x != ''])() |>
      paste0(collapse = x[length(x)]) |>
      parse(text = _) |>
      eval()
  }, MARGIN = 2) |>
  sum()
[1] 7450962489289