Advertisement
Guest User

04. Star Enigma

a guest
Sep 7th, 2020
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function decryptMsg(input) {
  2.     let msgCount = input.shift();
  3.     let pattern = /@(?<planetName>[A-Za-z]+)([^@\-:!>]*):[\d]+([^@\-:!>]*)!(?<attackType>[AD])!([^@\-:!>]*)\->[\d]+([^@\-:!>]*)/;
  4.     let attacked = [];
  5.     let destroyed = [];
  6.  
  7.     for (let i = 0; i < msgCount; i++) {
  8.         let line = input[i];
  9.         let decryptKey = line.match(/[star]/gi)
  10.             .length;
  11.         let decryptedMsg = [];
  12.         for (let letter of line) {
  13.             let symbol = letter.charCodeAt();
  14.             symbol -= decryptKey;
  15.             symbol = String.fromCharCode(symbol);
  16.             decryptedMsg.push(symbol);
  17.         }
  18.  
  19.         decryptedMsg = decryptedMsg.join("");
  20.         decryptedMsg = pattern.exec(decryptedMsg);
  21.  
  22.         if (decryptedMsg) {
  23.             if (decryptedMsg.groups.attackType === "A") {
  24.                 attacked.push(decryptedMsg.groups.planetName);
  25.             } else {
  26.                 destroyed.push(decryptedMsg.groups.planetName);
  27.             }
  28.         }
  29.     }
  30.  
  31.     attacked.sort((a, b) => a.localeCompare(b))
  32.     destroyed.sort((a, b) => a.localeCompare(b))
  33.  
  34.     console.log(`Attacked planets: ${attacked.length}`);
  35.     attacked.forEach(planet => console.log(`-> ${planet}`));
  36.     console.log(`Destroyed planets: ${destroyed.length}`)
  37.     destroyed.forEach(planet => console.log(`-> ${planet}`));
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement