Advent of Code 2020

4.R 492B

12345678910111213
  1. library(dplyr)
  2. library(stringr)
  3. library(tidyr)
  4. input <- readLines("input4.txt") %>% paste(collapse = "\n") %>% str_split("\n\n") %>% unlist() %>%str_replace_all("\n", " ")
  5. flds <- input %>% str_extract_all("[a-z]+:") %>% unlist() %>% unique() %>% str_replace(":", "")
  6. df <- data.frame(Orig = input)
  7. for (f in flds) {
  8. df[[f]] <- str_extract(input, paste0(f, ":[a-zA-Z0-9#]+")) %>% str_replace_all(paste0(f, ":"), "")
  9. }
  10. df.valid <- df %>% drop_na(-cid)
  11. str(df)
  12. str(df.valid)
  13. nrow(df.valid)