Advertisement
ErolKZ

Untitled

Nov 22nd, 2021
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1.  
  2. function solve(input) {
  3.  
  4.  
  5. // PQ@Alderaa1:30000!A!->20000
  6.  
  7. // @Cantonica:3000!D!->4000NM
  8.  
  9.  
  10. let pattern = /.*@(?<planet>[A-Za-z]+){1}[^@!\-:>]*:(?<population>\d+){1}[^@!\-:>]*!(?<action>[AD]){1}![^@!\-:>]*->(?<army>\d+){1}.*/m;
  11.  
  12. let arr3 = [];
  13.  
  14. let letterCheck = /[s,t,a,r]+/gi;
  15.  
  16.  
  17.  
  18. let obj = {
  19.  
  20. attack: [],
  21.  
  22. destr: []
  23.  
  24. };
  25.  
  26.  
  27.  
  28.  
  29. for (let i = 1; i <= input[0]; i++) {
  30.  
  31.  
  32. if (input[i].match(letterCheck) !== null) {
  33.  
  34.  
  35. let count = input[i].match(letterCheck).join('').length;
  36.  
  37. let arr = input[i].split('');
  38.  
  39. let arr2 = [];
  40.  
  41.  
  42.  
  43.  
  44. for (let j = 0; j < arr.length; j++) {
  45.  
  46. arr2.push(String.fromCharCode(arr[j].charCodeAt() - count));
  47.  
  48. }
  49.  
  50.  
  51. arr3.push(arr2.join(''));
  52.  
  53.  
  54.  
  55. if (arr3[arr3.length - 1].match(pattern) === null) {
  56.  
  57. arr3.pop();
  58.  
  59. }
  60.  
  61. } else {
  62.  
  63. arr3.push(input[i]);
  64.  
  65. }
  66.  
  67. }
  68.  
  69.  
  70.  
  71. // planet name -> planet population -> attack type -> soldier count
  72.  
  73. for (let i = 0; i < arr3.length; i++) {
  74.  
  75. let match = pattern.exec(arr3[i]);
  76.  
  77. if (match !== null) {
  78.  
  79. let [_, name, popul, attType, solCount] = match;
  80.  
  81. if (attType === 'A') {
  82.  
  83. obj.attack.push(name);
  84.  
  85. } else if (attType === 'D') {
  86.  
  87. obj.destr.push(name);
  88.  
  89. }
  90.  
  91.  
  92. }
  93.  
  94.  
  95. }
  96.  
  97. // console.log(obj);
  98.  
  99.  
  100. for (let key in obj) {
  101.  
  102. obj[key] = obj[key].sort((a, b) => a.localeCompare(b));
  103.  
  104. }
  105.  
  106.  
  107. for (let key in obj) {
  108.  
  109. // Attacked planets: {attackedPlanetsCount}
  110. // -> {planetName}
  111. // Destroyed planets: {destroyedPlanetsCount}
  112. // -> {planetName}
  113.  
  114.  
  115.  
  116. if (obj.hasOwnProperty(['attack'])) {
  117.  
  118. console.log(`Attacked planets: ${obj.attack.length}`);
  119.  
  120. if (obj.attack.length > 0) {
  121.  
  122. for (let el of obj.attack) {
  123.  
  124. console.log(`-> ${el}`);
  125.  
  126. }
  127.  
  128. }
  129.  
  130. delete obj.attack;
  131.  
  132. }
  133.  
  134.  
  135.  
  136. if (obj.hasOwnProperty(['destr'])) {
  137.  
  138. console.log(`Destroyed planets: ${obj.destr.length}`);
  139.  
  140. if (obj.destr.length > 0) {
  141.  
  142. for (let el of obj.destr) {
  143.  
  144. console.log(`-> ${el}`);
  145.  
  146. }
  147.  
  148. }
  149.  
  150. delete obj.destr;
  151.  
  152. }
  153.  
  154.  
  155.  
  156.  
  157. }
  158.  
  159.  
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement