Advertisement
Lulunga

Final Exam 03. The Isle of Man TT Race Alternative

Jul 28th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.   for (let line of input) {
  3.     let pattern = /^([#$%*&])([A-Za-z]+)\1=(\d+)!!([\w\W]*?)$/g;
  4.     let tokens = pattern.exec(line);
  5.  
  6.     if (tokens !== null) {
  7.       let [ racer, length, encryptedCode ] = tokens.slice(2);
  8.       length = Number(length);
  9.  
  10.       if (length === encryptedCode.length) {
  11.         let decryptedCode = decryptCode(encryptedCode, length);
  12.         console.log(`Coordinates found! ${racer} -> ${decryptedCode}`);
  13.         break;
  14.       } else {
  15.         console.log('Nothing found!');
  16.       }
  17.     } else {
  18.       console.log('Nothing found!');
  19.     }
  20.   }
  21.  
  22.   function decryptCode(encrypted, length) {
  23.     let decryptedCode = '';
  24.  
  25.     for (let i = 0; i < encrypted.length; i++) {
  26.       let ascii = encrypted.charCodeAt(i);
  27.       ascii += length;
  28.       decryptedCode += String.fromCharCode(ascii);
  29.     }
  30.  
  31.     return decryptedCode;
  32.   }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement