# Zihui Xiao, 2106354, MSC Data and Artificial Intelligence

# task2-1
# decrypted

# Polybius cipher
Polybius<-matrix(c(NA,"V",NA,"L",
                   "Z",NA,"J",NA,
                   "M","A","I",NA,
                   "D",NA,"S","C",
                   NA,"R","B",NA,
                   "K",NA,"P","U",
                   "W","E","Y","H",
                   "Q",NA,NA,"N",
                   NA,"T","F",NA,
                   "G","O",NA,"X"),nrow=4) 

# Add column names
colnames(Polybius)<-c("0","1","2","3","4","5","6","7","8","9")  
# Add row names
rownames(Polybius)<-c("0","1","2","3")  

Polybius  # display Polybius

# Input ciphertext
Ciphertext<-"24160 63616 14162 61935 14163 71602 26222 33719 18008"

# Remove spaces from string
Ciphertext<-gsub(" ","",Ciphertext)

# Using a WHILE loop

plaintext<-NULL   # Vector defined outside loop

i=1  # initialise loop counter

while (i<nchar(Ciphertext))
{
  # Determine the letters corresponding to the ciphertext and update the plaintext
  plaintext<-c(plaintext,Polybius[substring(Ciphertext,i,i),substring(Ciphertext,i+1,i+1)])
  
  # increment i
  i=i+2
}
  # Remove NA
  plaintext<-na.omit(plaintext)
  # Merge string
  plaintext<-Reduce('paste0',plaintext)
  
  # Display plaintext
  print(plaintext)     





