mat <- as.matrix(fread("grid2.csv", header = FALSE, na.strings = ""))
startX <<- 3
startY <<- 1
apply(dt, 1, function(x) {
sapply(unlist(stri_match_all_regex(x, "[ULRD]")), function(y) {
if (y == "L") {
startY <<- startY - 1
if (startY == 0 || is.na(mat[startX,startY])) { startY <<- startY + 1 }
} else if (y == "R") {
startY <<- startY + 1
if (startY == 6 || is.na(mat[startX,startY])) { startY <<- startY - 1 }
} else if (y == "U") {
startX <<- startX - 1
if (startX == 0 || is.na(mat[startX,startY])) { startX <<- startX + 1 }
} else {
startX <<- startX + 1
if (startX == 6 || is.na(mat[startX,startY])) { startX <<- startX - 1 }
}
})
return(rbind(startX, startY))
}) |>
t() |>
apply(1, function(x) {
return(mat[x[1], x[2]])
}) |>
paste0(collapse = "") |>
stri_replace_all_regex(" ", "")