Advertisement
Guest User

Untitled

a guest
Oct 20th, 2022
108
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.  
  7.     function decrypt(string) {
  8.         let regexCounter = /s|t|a|r/gi; // RegEx to extract 'star' letters
  9.         let result = string.match(regexCounter); // gives us an array with symbols matched the regexCounter
  10.         let numToDecrease = result.length; // count of star letters
  11.  
  12.         let array = string.split(''); // converting string to array
  13.         let ASCIIArray = array.map(symbol => symbol.charCodeAt(0)); // converting array symbols to ASCII code
  14.         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
  15.         let backToSymbols = decreasedASCIIArray.map(a => String.fromCharCode(a)); // converting new ASCII codes to symbols again
  16.  
  17.         return backToSymbols.join('');
  18.     }
  19.  
  20.     // loop through the input
  21.     for (let i = 0; i < count; i++) {
  22.         // parse encrypted message
  23.         let encryptedMessage = input[i];
  24.  
  25.         // decrypt it by aplying star enigma decription rules
  26.         let decryptedMessage = decrypt(encryptedMessage);
  27.  
  28.         // extract required elements from decrypted message by aplying RegEx with described rules
  29.         let regexMessage = /@(?<planetName>[A-Za-z]+)[^@\-!:>]*:(?<population>\d+)[^@\-!:>]*!(?<attackType>[A|D])![^@\-!:>]*\->(?<soldierCount>\d+)[^@\-!:>]*/g;
  30.         let result = regexMessage.exec(decryptedMessage);
  31.  
  32.         if (result !== null) {
  33.             let attack = result.groups.attackType;
  34.  
  35.             if (attack === 'A') {
  36.                 attackedPlanets.push(result.groups.planetName);
  37.             } else if (attack === 'D') {
  38.                 destroyedPlanets.push(result.groups.planetName);
  39.             }
  40.         }
  41.     }
  42.  
  43.     console.log(`Attacked planets: ${attackedPlanets.length}`);
  44.     attackedPlanets
  45.         .sort((a, b) => a.localeCompare(b)) // sorting alphabetically
  46.         .forEach(planet => console.log(`-> ${planet}`)); // print each planet of the attackedPlanets array
  47.     console.log(`Destroyed planets: ${destroyedPlanets.length}`);
  48.     destroyedPlanets
  49.         .sort((a, b) => a.localeCompare(b)) // sorting alphabetically
  50.         .forEach(planet => console.log(`-> ${planet}`)); // print each planet of the destroyedPlanets array
  51. }
  52.  
  53. starEnigma([
  54.     '2',
  55.     'STCDoghudd4=63333$D$0A53333',
  56.     'EHfsytsnhf?8555&I&2C9555SR',
  57.     'GQhrr|A977777(H(TTTT',
  58. ]);
  59. console.log('---');
  60. starEnigma([
  61.     '3',
  62.     "tt(''DGsvywgerx>6444444444%H%1B9444",
  63.     'GQhrr|A977777(H(TTTT',
  64.     'EHfsytsnhf?8555&I&2C9555SR'
  65. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement