Advertisement
ErolKZ

Untitled

Nov 22nd, 2021
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1.  
  2. function solve(input) {
  3. // PQ@Alderaa1:30000!A!->20000
  4.  
  5. // @Cantonica:3000!D!->4000NM
  6.  
  7. let pattern =
  8. /.*@(?<planet>[A-Za-z]+){1}[^@!\-:>]*:(?<population>\d+){1}[^@!\-:>]*!(?<action>[AD]){1}![^@!\-:>]*->(?<army>\d+){1}.*/m;
  9.  
  10. let arr3 = [];
  11.  
  12. let letterCheck = /[s,t,a,r]+/gi;
  13.  
  14. let obj = {
  15. attack: [],
  16.  
  17. destr: [],
  18. };
  19.  
  20. for (let i = 1; i <= input[0]; i++) {
  21. if (input[i].match(letterCheck) !== null) {
  22. let count = input[i].match(letterCheck).join("").length;
  23.  
  24. let arr = input[i].split("");
  25.  
  26. let arr2 = [];
  27.  
  28. for (let j = 0; j < arr.length; j++) {
  29. arr2.push(String.fromCharCode(arr[j].charCodeAt() - count));
  30. }
  31.  
  32. arr3.push(arr2.join(""));
  33.  
  34. if (arr3[arr3.length - 1].match(pattern) === null) {
  35. arr3.pop();
  36. }
  37. } else {
  38. arr3.push(input[i]);
  39. }
  40. }
  41.  
  42. // planet name -> planet population -> attack type -> soldier count
  43.  
  44. for (let i = 0; i < arr3.length; i++) {
  45. let match = pattern.exec(arr3[i]);
  46.  
  47. if (match !== null) {
  48. let [_, name, popul, attType, solCount] = match;
  49.  
  50. if (attType === "A") {
  51. obj.attack.push(name);
  52. } else if (attType === "D") {
  53. obj.destr.push(name);
  54. }
  55. }
  56. }
  57.  
  58. // console.log(obj);
  59.  
  60. for (let key in obj) {
  61. obj[key] = obj[key].sort((a, b) => a.localeCompare(b));
  62. }
  63.  
  64. for (let key in obj) {
  65. // Attacked planets: {attackedPlanetsCount}
  66. // -> {planetName}
  67. // Destroyed planets: {destroyedPlanetsCount}
  68. // -> {planetName}
  69.  
  70. if (obj.hasOwnProperty(["attack"])) {
  71. console.log(`Attacked planets: ${obj.attack.length}`);
  72.  
  73. if (obj.attack.length > 0) {
  74. for (let el of obj.attack) {
  75. console.log(`-> ${el}`);
  76. }
  77. }
  78.  
  79. delete obj.attack;
  80. }
  81.  
  82. if (obj.hasOwnProperty(["destr"])) {
  83. console.log(`Destroyed planets: ${obj.destr.length}`);
  84.  
  85. if (obj.destr.length > 0) {
  86. for (let el of obj.destr) {
  87. console.log(`-> ${el}`);
  88. }
  89. }
  90.  
  91. delete obj.destr;
  92. }
  93. }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement