Advertisement
bebo231312312321

Untitled

Mar 16th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function starEnigma(input) {
  2.     let count = Number(input.shift())
  3.     let planets = { attackCount: [],destroyedCounter: []}
  4.     let path = /[star]/gmi
  5.     input.forEach((element, index) => {
  6.         if (index < count) {
  7.              (element.match(path)) ? maches = Number(element.match(path).length):  maches = 0    
  8.             let messages = element.split("").map((e) => String.fromCharCode(e.charCodeAt(0) - maches)).join("")
  9.             let messagePathern = /@(?<name>[A-Za-z]+)[^@:!\->]*:(?<planet>\d+)[^@:!\->]*!(?<command>[AD])![^@:!\->]*->(?<atack>\d+)/gm
  10.             let matchesPath = messagePathern.exec(messages)
  11.  
  12.             if (matchesPath) {
  13.                 let { name, planet, command, attack } = matchesPath.groups
  14.                 if (command == "A") {
  15.                     planets.attackCount.push(name)
  16.                 } else if (command == "D") {
  17.                     planets.destroyedCounter.push(name)
  18.                 }
  19.             }
  20.         }
  21.     });
  22.     console.log(`Attacked planets: ${(planets.attackCount.length)}`)
  23.     if (planets.attackCount.length > 0) {
  24.         planets.attackCount.sort((a, b) => a.localeCompare(b)).forEach(element => console.log(`-> ${element}`))
  25.     }
  26.     console.log(`Destroyed planets: ${planets.destroyedCounter.length}`)
  27.     if (planets.destroyedCounter.length > 0) {
  28.         planets.destroyedCounter.sort((a, b) => a.localeCompare(b)).forEach(element => console.log(`-> ${element}`))
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement