Advertisement
Todorov_Stanimir

04. Star Enigma

Jul 17th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function starEnigma(input) {
  2.     let counter = Number(input.shift());
  3.     let pattern = /[@]{1}(?<name>[a-z]+)[^@\-!:>]*[:]{1}(?<population>[0-9]+)[^@\-!:>]*[!]{1}(?<typeAttack>[A\|D])[!]{1}[^@\-!:>]*->(?<soldierCount>[0-9]+)/gi;
  4.     let planets = {};
  5.     planets['A'] = [];
  6.     planets['D'] = [];
  7.  
  8.     for (let i = 1; i <= counter; i++) {
  9.         let line = input.shift();
  10.         let counterLattersSTAR = line
  11.             .split('')
  12.             .map(x => x.toLowerCase())
  13.             .filter(x => (x === 's' || x === 't' || x === 'a' || x === 'r'))
  14.             .length;
  15.         let decriptedMessage = line.split('').map(elem => String.fromCharCode(elem.charCodeAt() - counterLattersSTAR)).join('')
  16.         if (decriptedMessage.match(pattern)) {
  17.             let currentPlanet = pattern.exec(decriptedMessage);
  18.             planets[currentPlanet.groups['typeAttack']].push(currentPlanet.groups['name']);
  19.         }
  20.     }
  21.     Object.entries(planets)
  22.         .forEach((typeAttack) => {
  23.             if (typeAttack[0] === 'A') {
  24.                 console.log(`Attacked planets: ${typeAttack[1].length}`);
  25.             } else {
  26.                 console.log(`Destroyed planets: ${typeAttack[1].length}`);
  27.             }
  28.             typeAttack[1].sort((a, b) => a.localeCompare(b))
  29.                 .forEach(planet => console.log(`-> ${planet}`));
  30.         });
  31. }
  32. starEnigma(['2',
  33.     'STCDoghudd4=63333$D$0A53333',
  34.     'EHfsytsnhf?8555&I&2C9555SR']);
  35.  
  36. starEnigma(['3',
  37.     'tt(\'\'DGsvywgerx>6444444444%H%1B9444',
  38.     'GQhrr|A977777(H(TTTT',
  39.     'EHfsytsnhf?8555&I&2C9555SR']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement