Advent of Code 2020

10.R 808B

12345678910111213141516171819202122232425262728293031323334353637
  1. library(parallel)
  2. library(stringr)
  3. library(dplyr)
  4. input <- sort(as.numeric(readLines("input10.txt")))
  5. diffs <- c(diff(c(0, input)), 3)
  6. sum(diffs == 1) * sum(diffs == 3)
  7. maxv <- max(input) + 3
  8. # brute force is absolutely not possible
  9. # combn(input, 2, FUN = function(sel) {
  10. # all(unique(diff(c(0, sel, maxv))) %in% 1:3)
  11. # })
  12. # output <- mclapply(1:length(input),
  13. # function(m) {
  14. # combn(input, m, FUN = function(sel) {
  15. # all(unique(diff(c(0, sel, maxv))) %in% 1:3)
  16. # })
  17. # }, mc.cores = 7)
  18. ds<- paste0(diffs, collapse = "")
  19. dvec <- str_split(ds, "3+") %>% sapply(nchar) %>% as.numeric()
  20. pvec <- ifelse(dvec < 1, 1, 2^(dvec - 1))
  21. pvec <- ifelse(pvec == 8, 7, pvec)
  22. print(prod(pvec), digits = 12)
  23. # 14173478093824
  24. # 0 1
  25. # 1 1
  26. # 2 2
  27. # 3 4
  28. # 4 7