Advertisement
bebo231312312321

Untitled

Mar 19th, 2023
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function starEnigma(arr) {
  2.     let decryption = /[star]/gi
  3.     let planetRegex = /@(?<name>[A-Za-z]+)[^@!:>\-]*:(?<population>[0-9]+)[^@!:>\-]*!(?<attackType>[A|D])![^@!:>\-]*->(?<soldierCount>[0-9]+)/g
  4.     let numberOfMessages = arr.shift()
  5.     let newString = ""
  6.     let attackedPlanets = []
  7.     let destroyedPlanets = []
  8.     arr.forEach((message, index) => {
  9.         if (index < numberOfMessages) {
  10.            
  11.                 if(message.match(decryption)){
  12.                 match= message.match(decryption).length
  13.                 }else{
  14.                 match = 0
  15.                 }
  16.                 for(let letter of message){
  17.                 let letterCharCode = letter.charCodeAt(0) - match
  18.                 newString += String.fromCharCode(letterCharCode)
  19.                 }
  20.             let planet = planetRegex.exec(newString)
  21.             // newString = ""
  22.             // if(planet==null){      
  23.             //     continue  
  24.             // }
  25.             if(planet){
  26.             let planetName = planet.groups.name
  27.             let planetType = planet.groups.attackType
  28.             if (planetType === "A") {
  29.                 attackedPlanets.push(planetName)
  30.             } else if(planetType === "D") {
  31.                 destroyedPlanets.push(planetName)
  32.             }
  33.         }
  34.  
  35.         }
  36.     })
  37.  
  38.     console.log(`Attacked planets: ${attackedPlanets.length}`);
  39.     if(attackedPlanets.length>0 && attackedPlanets.length!= undefined){
  40.     logPlanets(attackedPlanets)
  41.     }
  42.     console.log(`Destroyed planets: ${destroyedPlanets.length}`);
  43.     if(destroyedPlanets.length>0&& destroyedPlanets.length!= undefined){
  44.     logPlanets(destroyedPlanets)
  45.     }
  46.     function logPlanets(arr) {
  47.         arr.sort((a, b) => a.localeCompare(b)).forEach(planet=> {
  48.             console.log(`-> ${planet}`);
  49.         })
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement