-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_13.R
39 lines (32 loc) · 1.15 KB
/
day_13.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
data13 <- readLines("Input/day13.txt")
data13 <- data13[data13 != ""]
split_el <- function(z) {
z1 <- strsplit(gsub("^\\[(.*)\\]$", "\\1", z), "")[[1]]
z2 <- cumsum(z1 == "[") - cumsum(c("", head(z1, -1)) == "]")
z1[z1 == "," & z2 == 0] <- ":"
strsplit(paste(z1, collapse = ""), ":")[[1]]
}
compare <- function(x, y) {
x1 <- split_el(x)
y1 <- split_el(y)
if (max(length(x1), length(y1)) > 0) {
for (k in 1:max(length(x1), length(y1))) {
if (length(x1) < k) return(TRUE)
if (length(y1) < k) return(FALSE)
if (!grepl("^\\[", x1[k]) & !grepl("^\\[", y1[k])) {
if (as.integer(x1[k]) < as.integer(y1[k])) return(TRUE)
if (as.integer(x1[k]) > as.integer(y1[k])) return(FALSE)
} else {
x2 <- if (grepl("^\\[", x1[k])) x1[k] else paste0("[", x1[k], "]")
y2 <- if (grepl("^\\[", y1[k])) y1[k] else paste0("[", y1[k], "]")
com <- compare(x2, y2)
if (!is.na(com)) return(com)
}
}
}
return(NA)
}
#part1-----
sum(which(sapply(1:150, \(k) compare(data13[k*2 - 1L], data13[k*2]))))
#part2---------
prod(sapply(c("[[2]]", "[[6]]"), \(y) sum(sapply(data13, compare, y = y))) + 1:2)