Advertisement
vladovip

RegExp_ Star Enigma_ JS FUND

Sep 4th, 2022
938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  // It gives me 80 points / out of 100. It should be refined.
  2.  
  3. function starEnigma(inputArr) {
  4.    
  5.   let numberMessg = inputArr.shift();
  6.   let currentDecryptKey = 0;
  7.   let resultArr = [];
  8.   for (let i = 0; i < numberMessg; i++) {
  9.     let pattern = /[starSTAR]/g;
  10.     let matchArr = inputArr[i].match(pattern);
  11.     // console.log(matchArr)
  12.    if (matchArr != null) {
  13.       let decryptedMessage = '';
  14.       currentDecryptKey = matchArr.length;
  15.     //   console.log(currentDecryptKey);
  16.       let tempArrMessage = inputArr[i].split("");
  17.     //   console.log(tempArrMessage);
  18.       for (let el of tempArrMessage) {
  19.         let newCharCode = el.charCodeAt() - currentDecryptKey;
  20.         let newSymbol = String.fromCharCode(newCharCode);
  21.         decryptedMessage += newSymbol;
  22.       }
  23.       resultArr.push(decryptedMessage);
  24.     }
  25.   }
  26.  
  27.   //   console.log(resultArr);
  28.   // [ 'PQ@Alderaa1:30000!A!->20000', '@Cantonica:3000!D!->4000NM' ]
  29.  
  30.   let attackedPlanetCounter = 0;
  31.   let attackePlanetArr = [];
  32.   let destroyedPlanetCounter = 0;
  33.   let destroyedPlanetArr = [];
  34.   for ( let el  of resultArr){
  35.  
  36.     let planetInfo = el;
  37.     let patternPlanet = /@(?<planetName>[A-Za-z]+)[^@\-!:and]*:(?<planetPopulation>\d+)[^@\-!:and]*!(?<attackType>[AD])![^@\-!:and]*->(?<soldierCount>\d+)/g;
  38.     let matchCollection = patternPlanet.exec(planetInfo);
  39.  
  40.     if ( matchCollection != null  ){
  41.         let currentPlanet = matchCollection.groups.planetName;
  42.         let population = Number(matchCollection.groups.planetPopulation);
  43.         let typeAttack = matchCollection.groups.attackType;
  44.         let soldierNums = Number(matchCollection.groups.soldierCount);
  45.         // console.log(currentPlanet,population,typeAttack,soldierNums);
  46.         // Alderaa 30000 A 20000
  47.         // Cantonica 3000 D 4000
  48.         //Coruscant 2000000000 D 5000
  49.        
  50.         if ( typeAttack == "A" ){
  51.             attackedPlanetCounter++;
  52.             attackePlanetArr.push(currentPlanet);
  53.         } else if (typeAttack == "D"){
  54.             destroyedPlanetCounter++;
  55.             destroyedPlanetArr.push(currentPlanet);
  56.         }
  57.     }
  58.   }
  59.      let sortedAttackedPlanets = attackePlanetArr.sort();
  60.      let sortedDestroyedPlanets = destroyedPlanetArr.sort();
  61.  
  62.      console.log(`Attacked planets: ${attackedPlanetCounter}`);
  63.      sortedAttackedPlanets.forEach(x => console.log(`-> ${x}`));
  64.      console.log(`Destroyed planets: ${destroyedPlanetCounter}`);
  65.      sortedDestroyedPlanets.forEach(x => console.log(`-> ${x}`));
  66.  
  67.  
  68. }
  69.  
  70.  
  71. starEnigma(["2", "STCDoghudd4=63333$D$0A53333", "EHfsytsnhf?8555&I&2C9555SR"]);
  72.  
  73. console.log(`*******`);
  74.  
  75. starEnigma([
  76.   "3",
  77.   "tt(''DGsvywgerx>6444444444%H%1B9444",
  78.   "GQhrr|A977777(H(TTTT",
  79.   "EHfsytsnhf?8555&I&2C9555SR",
  80. ]);
  81.  
  82.  
  83.  
  84. // @(?<planetName>[A-Za-z]+)[^@\-!:and]*:(?<planetPopulation>\d+)[^@\-!:and]*!(?<attackType>[AD])![^@\-!:and]*->(?<soldierCount>\d+)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement