Advertisement
bebo231312312321

Untitled

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