Advertisement
Guest User

Untitled

a guest
Jul 27th, 2020
1,127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function starEnigma(input) {
  2.  
  3.     let numberMessages = Number(input.shift());
  4.     let planets = [];
  5.     let patternStar = /[star]/g;
  6.  
  7.     for (let command of input) {
  8.         let counter = 0;
  9.         let splitArray = command.split("");
  10.         for (let i = 0; i < splitArray.length; i++) {
  11.             let currentChar = splitArray[i].toLowerCase();
  12.             if (currentChar.match(patternStar)) {
  13.                 counter++;
  14.             }
  15.         }
  16.  
  17.         for (let i = 0; i < splitArray.length; i++) {
  18.             let charCode = splitArray[i].charCodeAt(0);
  19.             let result = String.fromCharCode(charCode - counter);
  20.             splitArray[i] = result;
  21.         }
  22.  
  23.         let finalString = splitArray.join("");
  24.         planets.push(finalString)
  25.     }
  26.  
  27.     let attackedCount = 0;
  28.     let attackedPlanets = [];
  29.     let destroyedCount = 0;
  30.     let destroyedPlanets = [];
  31.     let patternPlanetMessage = /@(?<name>[A-Z][a-z]+)[^-@!>\W]*:(?<population>[\d]+)[^-@!>\d]*!(?<command>[AD])![^-@!>]*->(?<soldiers>[\d]+)/g;
  32.  
  33.     let planetInfo = patternPlanetMessage.exec(planets);
  34.  
  35.     while (planetInfo !== null) {
  36.         let name = planetInfo.groups['name'];
  37.         let attackCommand = planetInfo.groups['command'];
  38.  
  39.         switch (attackCommand) {
  40.             case "A":
  41.                 attackedCount++
  42.                 attackedPlanets.push(name);
  43.                 break;
  44.  
  45.             case "D":
  46.                 destroyedCount++;
  47.                 destroyedPlanets.push(name);
  48.                 break;
  49.         }
  50.  
  51.         planetInfo = patternPlanetMessage.exec(planets);
  52.     }
  53.  
  54.     attackedPlanets.sort((a, b) => a.localeCompare(b));
  55.     destroyedPlanets.sort((a, b) => a.localeCompare(b));
  56.  
  57.     console.log(`Attacked planets: ${attackedCount}`);
  58.     if (attackedPlanets.length > 0) {
  59.         for (let planet of attackedPlanets) {
  60.             console.log(`-> ${planet}`);
  61.         }
  62.     }
  63.     console.log(`Destroyed planets: ${destroyedCount}`);
  64.     if (destroyedPlanets.length > 0) {
  65.         for (let planet of destroyedPlanets) {
  66.             console.log(`-> ${planet}`);
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement