# Task 1
# 1
Polybius <- matrix(c("", "Z", "M", "D", "", "K", "W", "Q", "", "G",
                     "V", "", "A", "", "R", "", "E", "", "T", "O",
                     "", "J", "I", "S", "B", "P", "Y", "", "F", "",
                     "L", "", "", "C", "", "U", "H", "N", "", "X"), nrow=4, byrow = T)
colnames(Polybius) <- 0:9
rownames(Polybius) <- 0:3
Polybius

# 2
ciphertext <- c()
message <- "CAPTURETHEHILL"
for(i in 1:nchar(message)){
  ch <- substring(message, i, i)
  for(row in 1:nrow(Polybius)){
    for(col in 1:ncol(Polybius)){
      if(Polybius[row, col]==ch){
        ciphertext <- c(ciphertext, paste0(rownames(Polybius)[row], colnames(Polybius)[col]))
      }
    }
  }
}
ciphertext

# 3
finaltext <- paste0(ciphertext, collapse = "")
print(finaltext)
if(nchar(finaltext)%%5!=0){
  x <- 5-nchar(finaltext)%%5
  finaltext <- paste0(finaltext, paste0(sample(0:9, x, replace = T), collapse = ""), collapse = "")
}

text <- ""
for(i in 1:nchar(finaltext)){
  text <- paste0(text, substring(finaltext, i, i))
  if(i%%5==0 && i!=nchar(finaltext)){
    text <- paste0(text, ' ')
  }
}
finaltext <- text
finaltext
