Advertisement
ErolKZ

Untitled

Nov 22nd, 2021
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 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 = /\@([A-Z][a-z]+).*?:(\d+)\!(A|D)\!.*?\->(\d+)/;
  11.  
  12. let arr3 = [];
  13.  
  14. let letterCheck = /[s,t,a,r]+/gmi;
  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. let count = input[i].match(letterCheck).join('').length;
  33.  
  34. let arr = input[i].split('');
  35.  
  36. let arr2 = [];
  37.  
  38.  
  39.  
  40.  
  41. for (let j = 0; j < arr.length; j++) {
  42.  
  43. arr2.push(String.fromCharCode(arr[j].charCodeAt() - count));
  44.  
  45. }
  46.  
  47.  
  48. arr3.push(arr2.join(''));
  49.  
  50.  
  51. if (arr3[arr3.length - 1].match(pattern) === null) {
  52.  
  53. arr3.pop();
  54.  
  55. }
  56.  
  57.  
  58.  
  59. }
  60.  
  61.  
  62.  
  63. // planet name -> planet population -> attack type -> soldier count
  64.  
  65. for (let i = 0; i < arr3.length; i++) {
  66.  
  67. let match = pattern.exec(arr3[i]);
  68.  
  69. if (match !== null) {
  70.  
  71. let [_, name, popul, attType, solCount] = match;
  72.  
  73. if (attType === 'A') {
  74.  
  75. obj.attack.push(name);
  76.  
  77. } else if (attType === 'D') {
  78.  
  79. obj.destr.push(name);
  80.  
  81. }
  82.  
  83.  
  84. }
  85.  
  86.  
  87. }
  88.  
  89.  
  90.  
  91.  
  92. for (let key in obj) {
  93.  
  94. // Attacked planets: {attackedPlanetsCount}
  95. // -> {planetName}
  96. // Destroyed planets: {destroyedPlanetsCount}
  97. // -> {planetName}
  98.  
  99.  
  100. if (obj.hasOwnProperty(['attack'])) {
  101.  
  102. console.log(`Attacked planets: ${obj.attack.length}`);
  103.  
  104. if (obj.attack.length > 0) {
  105.  
  106. for (let el of obj.attack) {
  107.  
  108. console.log(`-> ${el}`);
  109.  
  110. }
  111.  
  112. }
  113.  
  114. delete obj.attack;
  115.  
  116. }
  117.  
  118.  
  119.  
  120. if (obj.hasOwnProperty(['destr'])) {
  121.  
  122. console.log(`Destroyed planets: ${obj.destr.length}`);
  123.  
  124. if (obj.destr.length > 0) {
  125.  
  126. for (let el of obj.destr) {
  127.  
  128. console.log(`-> ${el}`);
  129.  
  130. }
  131.  
  132. }
  133.  
  134. delete obj.destr;
  135.  
  136. }
  137.  
  138.  
  139.  
  140.  
  141. }
  142.  
  143.  
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement