Advertisement
Todorov_Stanimir

03. Star Enigma Fundamentals Exam - 04 March 2018 Part II

Jul 26th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. startEnigma = (input) => {
  2.     let result = {};
  3.     result['A'] = [];
  4.     result['D'] = [];
  5.     for (let i = 1; i <= input[0]; i++) {
  6.         let countStar = input[i].split('').filter(el => el.toLowerCase() === 's' || el.toLowerCase() === 't' || el.toLowerCase() === 'a' || el.toLowerCase() === 'r').length;
  7.         let decreptedMessage = input[i].split('').map(el => String.fromCharCode(el.charCodeAt(0) - countStar)).join('');
  8.         let pattern = /@(?<name>[A-Za-z]+)[^@\-!:>]*:(?<popul>[\d]+)[^@\-!:>]*!(?<type>[AD])![^@\-!:>]*->(?<count>[\d]+)/g;
  9.         if (decreptedMessage.match(pattern)) {
  10.             let message = pattern.exec(decreptedMessage);
  11.             (message.groups.type === 'A') ? result['A'].push(message.groups.name) : result['D'].push(message.groups.name);
  12.         }
  13.     }
  14.     Object.entries(result).forEach(type => {
  15.         console.log(type[0] === 'A' ? `Attacked planets: ${type[1].length}` : `Destroyed planets: ${type[1].length}`);
  16.  
  17.         type[1].sort((a, b) => a.localeCompare(b)).forEach(planet => console.log(`-> ${planet}`));
  18.     })
  19. }
  20. startEnigma([2,
  21.     'STCDoghudd4=63333$D$0A53333',
  22.     'EHfsytsnhf?8555&I&2C9555SR'
  23. ])
  24. startEnigma([3,
  25.     'tt(\'\'DGsvywgerx>6444444444%H%1B9444',
  26.     'GQhrr|A977777(H(TTTT',
  27.     'EHfsytsnhf?8555&I&2C9555SR'
  28. ])
  29. // Results:
  30. // Attacked planets: 1
  31. // -> Alderaa
  32. // Destroyed planets: 1
  33. // -> Cantonica
  34. // Attacked planets: 0
  35. // Destroyed planets: 2
  36. // -> Cantonica
  37. // -> Coruscant
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement