── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0 ✔ purrr 0.3.5
✔ tibble 3.1.8 ✔ dplyr 1.0.10
✔ tidyr 1.2.1 ✔ forcats 0.5.2
✔ readr 2.1.3
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
2021-10: Syntax Scoring
Part 1
dt2 <- dt
monkeyBall <- function(x, n) {
x <- str_replace_all(x, "\\(\\)|\\[\\]|\\{\\}|\\<\\>", "")
if(nchar(x) == n) {
t <- as.character(str_split(str_replace_all(x, "[\\(\\[\\{\\<]", ""),
"",
simplify = TRUE)[1,])
t <- t[1]
return(t)
}
return(monkeyBall(x, nchar(x)))
}
t <- c()
for(r in 1:nrow(dt2)) {
x <- dt2[r,1]
n <- nchar(x)
t <- append(t, monkeyBall(x, n))
}
t <- table(t)
if (is.na(t[")"]))
t[")"] <- 0
if (is.na(t["]"]))
t["]"] <- 0
if (is.na(t["}"]))
t["}"] <- 0
if (is.na(t[">"]))
t[">"] <- 0
(3 * t[")"]) + (57 * t["]"]) + (1197 * t["}"]) + (25137 * t[">"])
)
311949
Part 2
dt2 <- dt
monkeyBall <- function(x, n) {
x <- str_replace_all(x, "\\(\\)|\\[\\]|\\{\\}|\\<\\>", "")
if(nchar(x) == n) {
if(!grepl("\\)|\\]|\\}|\\>", x, perl = TRUE))
return(x)
return("")
}
return(monkeyBall(x, nchar(x)))
}
t <- c()
for(r in 1:nrow(dt2)) {
x <- dt2[r,1]
n <- nchar(x)
t <- append(t, monkeyBall(x, n))
}
t <- t[nchar(t) > 0]
s <- c()
for(i in t) {
miniS <- 0
miniT <- rev(str_split(i, "", simplify = TRUE)[1,])
for(l in miniT) {
miniS <- (5 * miniS) + case_when(l == "(" ~ 1,
l == "[" ~ 2,
l == "{" ~ 3,
l == "<" ~ 4)
}
s <- append(s, miniS)
}
s <- s[!is.na(s)]
median(s)
[1] 3042730309