Advertisement
dimoBs

imitation game

Feb 23rd, 2021
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 01. The Imitation Game!!!
  2. function solve(input) {
  3.  
  4.     let message = input.shift().split('');
  5.  
  6.     for (let key of input) {
  7.         let line = key.split('|');
  8.  
  9.         if (line[0] === "Move") {
  10.             let numbersOfLetters = Number(line[1]);
  11.             let copySymbol = message.slice(0, numbersOfLetters);
  12.             message.splice(0, numbersOfLetters);
  13.  
  14.             for (let x in copySymbol) {
  15.                 let symbolPush = copySymbol[x]
  16.                 message.push(symbolPush);
  17.             }
  18.  
  19.         } else if (line[0] === 'ChangeAll') {
  20.             message = message.map(x => x.replace(line[1], line[2]));
  21.  
  22.         } else if (line[0] === 'Insert') {
  23.             let index = Number(line[1]);
  24.             let value = line[2];
  25.             message.splice(index, 0, value);
  26.  
  27.         } else if (line[0] === 'Decode') {
  28.             console.log(`The decrypted message is: ${message.join('')}`);
  29.         }
  30.     }
  31. }
  32. solve(['zzHe', 'ChangeAll|z|l', 'Insert|2|o', 'Move|3', 'Decode']);
  33. solve(['owyouh', 'Move|2', 'Move|3', 'Insert|3|are', 'Insert|9|?', 'Decode']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement