Advent of Code 2020

2.R 455B

123456789101112131415161718
  1. library(stringr)
  2. input <- readLines("input2.txt")
  3. itab <- sapply(letters, function(l) { str_count(input, l)})
  4. w2 <- apply(itab, 1, function(r) {2 %in% r})
  5. w3 <- apply(itab, 1, function(r) {3 %in% r})
  6. sum(w2) * sum(w3)
  7. am <- sapply(input, function(x) {
  8. l <- nchar(x)
  9. sapply(1:l, function(i) {
  10. paste0(str_sub(x, 0, i-1), ".", str_sub(x, i+1))
  11. })
  12. })
  13. ctab <- table(as.character(am))
  14. which.max(ctab)