In [1]:
library(stringr)
In [2]:
inputString <- "^.....^.^^^^^.^..^^.^.......^^..^^^..^^^^..^.^^.^.^....^^...^^.^^.^...^^.^^^^..^^.....^.^...^.^.^^.^"
sizeQ1 <- 40
sizeQ2 <- 400000
In [3]:
computeTheShit <- function(input, size){
latest <- c(".", strsplit(input,"")[[1]], "." )
total <- list(latest)
while(length(total)<size){
tmp <- "."
for(i in 2:(length(latest)-1)){
#tmp <- c(tmp,latest[i])
upper <- paste(latest[(i-1):(i+1)],collapse="")
#print(upper)
switch(upper,
"^^."={
tmp <- c(tmp,'^')
},
".^^"={
tmp <- c(tmp,'^')
},
"^.."={
tmp <- c(tmp,'^')
},
"..^"={
tmp <- c(tmp,'^')
},
{
tmp <- c(tmp,'.')
}
)
}
tmp <- c(tmp,".")
total[[length(total)+1]] <- tmp
latest <- tmp
}
return(sum(str_count(total,"\\."))-length(total)*2)#sum(str_count(total,"\\."))-length(total)*2
}
In [4]:
Q1 <- computeTheShit(inputString, sizeQ1)
Q1
In [5]:
Q2 <- computeTheShit(inputString, sizeQ2)
Q2
In [6]:
cat("Q1- Shortest path to the vault is : ", Q1, '\n')
cat("Q2- Longest path to the vault is : ", Q2, '\n')