Advertisement
Guest User

Untitled

a guest
Oct 20th, 2022
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function starEnigma(input) {
  2.     // parse how many teams we should loop through the input
  3.     let count = Number(input.shift());
  4.     let attackedPlanets = [];
  5.     let destroyedPlanets = [];
  6.     let decryptedStringsArr = [];
  7.  
  8.     function decrypt(string) {
  9.         let regexCounter = /s|t|a|r/gi; // RegEx to extract 'star' letters
  10.         let result = string.match(regexCounter); // gives us an array with symbols matched the regexCounter
  11.         let numToDecrease = result.length; // count of star letters
  12.  
  13.         let array = string.split(''); // converting string to array
  14.         let ASCIIArray = array.map(symbol => symbol.charCodeAt(0)); // converting array symbols to ASCII code
  15.         let decreasedASCIIArray = ASCIIArray.map(a => a - numToDecrease); // reducing each ASCII code by count of matched 'star' letters(e.g. decryption key) in the initial message
  16.         let backToSymbols = decreasedASCIIArray.map(a => String.fromCharCode(a)); // converting new ASCII codes to symbols again
  17.  
  18.         return backToSymbols.join('');
  19.     }
  20.  
  21.     for (let i = 0; i < count; i++) {
  22.         let decryptedString = decrypt(input[i]);
  23.         decryptedStringsArr.push(decryptedString);
  24.     }
  25.  
  26.     let decryptedStrings = decryptedStringsArr.join(' ');
  27.  
  28.     let regex = /@(?<planetName>[A-Za-z]+)[^@\-!:>]*:(?<population>\d+)[^@\-!:>]*!(?<attackType>[A|D])![^@\-!:>]*\->(?<soldierCount>\d+)[^@\-!:>]*/g;
  29.     let result = '';
  30.  
  31.     while ((result = regex.exec(decryptedStrings)) !== null) {
  32.         let planetName = result.groups.planetName;
  33.  
  34.         switch (result.groups.attackType) {
  35.             case 'A': {
  36.                 attackedPlanets.push(planetName);
  37.             };
  38.                 break;
  39.             case 'D': {
  40.                 destroyedPlanets.push(planetName);
  41.             };
  42.                 break;
  43.         }
  44.     }
  45.  
  46.     attackedPlanets.sort((a, b) => a.localeCompare(b));
  47.     destroyedPlanets.sort((a, b) => a.localeCompare(b));
  48.  
  49.     console.log(`Attacked planets: ${attackedPlanets.length}`);
  50.     attackedPlanets.forEach(planet => console.log(`-> ${planet}`));
  51.     console.log(`Destroyed planets: ${destroyedPlanets.length}`);
  52.     destroyedPlanets.forEach(planet => console.log(`-> ${planet}`));
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement