Advertisement
kstoyanov

04. Star Enigma js fundamentals

Jul 28th, 2020 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.   const [, ...msgArr] = args;
  3.   const rePat = /[@]{1}(?<name>[a-z]+)[^@\-!:>]*[:]{1}(?<population>[0-9]+)[^@\-!:>]*[!]{1}(?<typeAttack>[A|D])[!]{1}[^@\-!:>]*->(?<soldierCount>[0-9]+)/gi;
  4.  
  5.   const attackedPlanets = [];
  6.   const destroyedPlanets = [];
  7.  
  8.   msgArr.forEach((line) => {
  9.     const counterLet = line
  10.       .split('')
  11.       .map((x) => x.toLowerCase())
  12.       .filter((x) => (x === 's' || x === 't' || x === 'a' || x === 'r'))
  13.       .length;
  14.     const decriptedMsg = line.split('').map((elem) => String.fromCharCode(elem.charCodeAt() - counterLet)).join('');
  15.     if (decriptedMsg.match(rePat)) {
  16.       const currentPlanet = rePat.exec(decriptedMsg);
  17.       if (currentPlanet.groups.typeAttack === 'A') {
  18.         attackedPlanets.push(currentPlanet.groups.name);
  19.       } else if (currentPlanet.groups.typeAttack === 'D') {
  20.         destroyedPlanets.push(currentPlanet.groups.name);
  21.       }
  22.     }
  23.   });
  24.  
  25.  
  26.   console.log(`Attacked planets: ${attackedPlanets.length}`);
  27.   if (attackedPlanets !== 0) {
  28.     attackedPlanets
  29.       .sort((a, b) => a.localeCompare(b))
  30.       .forEach((planet) => console.log(`-> ${planet}`));
  31.   }
  32.  
  33.   console.log(`Destroyed planets: ${destroyedPlanets.length}`);
  34.   if (destroyedPlanets !== 0) {
  35.     destroyedPlanets
  36.       .sort((a, b) => a.localeCompare(b))
  37.       .forEach((planet) => console.log(`-> ${planet}`));
  38.   }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement