|
@@ -1,13 +1,25 @@
|
1
|
1
|
library(dplyr)
|
2
|
2
|
library(stringr)
|
3
|
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(":", "")
|
|
4
|
+input <- readLines("input4.txt") %>% paste(collapse = "\n") %>% str_split("\n\n") %>% unlist() %>% str_replace_all("\n", " ")
|
|
5
|
+flds <- str_match(input, "([a-z]+):")[,2] %>% unique()
|
6
|
6
|
df <- data.frame(Orig = input)
|
7
|
7
|
for (f in flds) {
|
8
|
|
- df[[f]] <- str_extract(input, paste0(f, ":[a-zA-Z0-9#]+")) %>% str_replace_all(paste0(f, ":"), "")
|
|
8
|
+ df[[f]] <- str_match(input, paste0(f, ":([a-zA-Z0-9#]+)"))[,2]
|
9
|
9
|
}
|
10
|
10
|
df.valid <- df %>% drop_na(-cid)
|
11
|
11
|
str(df)
|
12
|
12
|
str(df.valid)
|
13
|
13
|
nrow(df.valid)
|
|
14
|
+
|
|
15
|
+ecs <- c("amb", "blu", "brn", "gry", "grn", "hzl", "oth")
|
|
16
|
+df.valid %>% mutate(
|
|
17
|
+ byr = ifelse(between(as.numeric(str_match(byr, "^([0-9]+)$")[,2]), 1920, 2002), byr, NA),
|
|
18
|
+ iyr = ifelse(between(as.numeric(str_match(iyr, "^([0-9]+)$")[,2]), 2010, 2020), iyr, NA),
|
|
19
|
+ eyr = ifelse(between(as.numeric(str_match(eyr, "^([0-9]+)$")[,2]), 2020, 2030), eyr, NA),
|
|
20
|
+ hgt = ifelse(between(as.numeric(str_match(hgt, "^([0-9]+)cm$")[,2]), 150, 193) | between(as.numeric(str_match(hgt, "^([0-9]+)in$")[,2]), 59, 76), hgt, NA),
|
|
21
|
+ hcl = str_extract(hcl, "^[#]{1}[0-9a-f]{6}$"),
|
|
22
|
+ ecl = ifelse(ecl %in% ecs, ecl, NA),
|
|
23
|
+ pid = str_extract(pid, "^[0-9]{9}$")
|
|
24
|
+ ) %>% drop_na(-cid) -> dv2
|
|
25
|
+nrow(dv2)
|