In [6]:
library(foreach)
library(stringr)
library(binhf)
In [2]:
input <- readLines("data.txt")
In [3]:
password <- c('a','b','c','d','e','f','g','h')
#password <- c('a','b','c','d','e')
a <- foreach(i=input) %do% {
cmd <- str_split(i," ")[[1]]
switch(paste(c(cmd[[1]],cmd[[2]]),collapse=""),
swapposition={
tmp <- password[[as.integer(cmd[[3]])+1]]
password[[as.integer(cmd[[3]])+1]] <- password[[as.integer(cmd[[6]])+1]]
password[[as.integer(cmd[[6]])+1]] <- tmp
# print(password)
},
swapletter={
tmp <- cmd[[3]]
tmp2 <- cmd[[6]]
password <- str_replace(password, cmd[[3]], 'X')
password <- str_replace(password, cmd[[6]], tmp)
password <- str_replace(password, 'X', tmp2)
# print(password)
},
reversepositions={
from <- as.integer(cmd[[3]])+1
to <- as.integer(cmd[[5]])+1
password[from:to] <- rev(password[from:to])
# print(password)
},
rotateleft={
password <- shift(password,as.integer(cmd[[3]]),dir = "left")
# print(password)
},
rotateright={
password <- shift(password,as.integer(cmd[[3]]),dir = "right")
# print(password)
},
moveposition={
from <- as.integer(cmd[[3]])+1
to <- as.integer(cmd[[6]])
tmp <- password[[from]]
password <- append(password[-from],tmp,to)
#print(password)
},
rotatebased={
value <- which(password == cmd[[7]]) - 1
value <- value + if(value >= 4) 2 else 1
# print(value)
password <- shift(password, value%%length(password), dir = "right")
# print(password)
},
{
print('default')
}
)
# print(paste(password,collapse=""))
}
Q1 <- paste(password,collapse="")
In [4]:
password <- c('f','b','g','d','c','e','a','h')
#password <- c('a','b','c','d','e')
#paste(password,collapse="")
a <- foreach(i=rev(input)) %do% {
cmd <- str_split(i," ")[[1]]
switch(paste(c(cmd[[1]],cmd[[2]]),collapse=""),
swapposition={
tmp <- password[[as.integer(cmd[[3]])+1]]
password[[as.integer(cmd[[3]])+1]] <- password[[as.integer(cmd[[6]])+1]]
password[[as.integer(cmd[[6]])+1]] <- tmp
# print(password)
},
swapletter={
tmp <- cmd[[3]]
tmp2 <- cmd[[6]]
password <- str_replace(password, cmd[[3]], 'X')
password <- str_replace(password, cmd[[6]], tmp)
password <- str_replace(password, 'X', tmp2)
# print(password)
},
reversepositions={
from <- as.integer(cmd[[3]])+1
to <- as.integer(cmd[[5]])+1
password[from:to] <- rev(password[from:to])
# print(password)
},
rotateleft={
password <- shift(password,as.integer(cmd[[3]]),dir = "right")
# print(password)
},
rotateright={
password <- shift(password,as.integer(cmd[[3]]),dir = "left")
# print(password)
},
moveposition={
from <- as.integer(cmd[[6]])+1
to <- as.integer(cmd[[3]])
tmp <- password[[from]]
password <- append(password[-from],tmp,to)
#print(password)
},
rotatebased={
tmp <- password
while(TRUE){
password <- shift(password, 1, dir = "left")
value <- which(password == cmd[[7]]) - 1
value <- value + if(value >= 4) 2 else 1
if(all(shift(password, value%%length(password), dir = "right")==tmp))
break
}
},
{
print('default')
}
)
#print(paste(password,collapse=""))
}
Q2 <- paste(password,collapse="")
Q2
In [5]:
cat("Q1- The first password is : ", Q1, '\n')
cat("Q2- The original input for the second password is : ", Q2, '\n')