17: No Such Thing as Too Much

library(mistlecode)
To install `mistlecode` yourself, run `devtools::install_github('guslipkin/mistlecode')`.

 Also loading:  cipheR data.table dplyr purrr slider stringr tidyverse glue
dt <- 
  readLines("input.txt") |>
  as.integer() |>
  sort(decreasing = TRUE)

buckets <-
  dt |>
  seq_along() |>
  map_int(\(x) {
    x <-
      dt |>
      combn(x) |>
      data.frame() |>
      map_int(sum)
    x <- sum(x == 150)
    if (x == 0) NA else x
  })

buckets was made for part 1, but is used in part 2 as well.

Part 1

This was surprisingly easy. I thought for sure I was going to have to run stuff in parallel.

sum(buckets, na.rm = TRUE)
[1] 4372

Part 2

Also weirdly easy. Especially since I set myself up for that with part 1.

buckets |>
  na.omit() |>
  head(1)
[1] 4