Advertisement
ErolKZ

Untitled

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