Advertisement
ErolKZ

Untitled

Dec 28th, 2021
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. function solve(input) {
  2. let arr = additionalSorting();
  3.  
  4. // console.log(arr[1]);
  5.  
  6. for (let el of arr) {
  7. console.log(`${el.shift()}: ${el[0].shift()[1]} skill`);
  8.  
  9. // console.log(el);
  10.  
  11. for (let el2 of el) {
  12. // console.log(el2);
  13.  
  14. for (let el3 of el2) {
  15. console.log(`- ${el3[0]} <!> ${el3[1]}`);
  16. }
  17. }
  18. }
  19.  
  20. function objectCreation(input) {
  21. let gladObj = {};
  22.  
  23. while (true) {
  24. if (input[0].split(" -> ").length === 1) {
  25. break;
  26. }
  27.  
  28. let cur = input.shift().split(" -> ");
  29.  
  30. let name = cur[0];
  31.  
  32. let technique = cur[1];
  33.  
  34. let skill = Number(cur[2]);
  35.  
  36. if (gladObj.hasOwnProperty(name)) {
  37. if (gladObj[name].hasOwnProperty(technique)) {
  38. if (gladObj[name][technique] < skill) {
  39. gladObj[name][technique] = skill;
  40. }
  41. } else {
  42. gladObj[name][technique] = skill;
  43. }
  44. } else {
  45. gladObj[name] = { [technique]: skill };
  46. }
  47. }
  48.  
  49. return gladObj;
  50. }
  51.  
  52. function totalCalc() {
  53. let gladObj = objectCreation(input);
  54.  
  55. for (let key in gladObj) {
  56. let total = 0;
  57.  
  58. for (let key2 in gladObj[key]) {
  59. total += gladObj[key][key2];
  60. }
  61.  
  62. gladObj[key]["Total"] = total;
  63. }
  64.  
  65. return gladObj;
  66. }
  67.  
  68. function fights(input) {
  69. let gladObj = totalCalc();
  70.  
  71. for (let el of input) {
  72. let cur = el.split(" vs ");
  73.  
  74. let gladOne = cur[0];
  75.  
  76. let gladTwo = cur[1];
  77.  
  78. if (gladObj.hasOwnProperty(gladOne) && gladObj.hasOwnProperty(gladTwo)) {
  79. let gladOneTech = Object.keys(gladObj[gladOne]);
  80.  
  81. let gladTwoTech = Object.keys(gladObj[gladTwo]);
  82.  
  83. let bool = compare(gladOneTech, gladTwoTech);
  84.  
  85. if (bool) {
  86. if (gladObj[gladOne]["Total"] > gladObj[gladTwo]["Total"]) {
  87. delete gladObj[gladTwo];
  88. } else {
  89. delete gladObj[gladOne];
  90. }
  91. }
  92. }
  93. }
  94.  
  95. return gladObj;
  96. }
  97.  
  98. function compare(gladOneTech, gladTwoTech) {
  99. let bool = gladOneTech.some((el) =>
  100. el !== "Total" ? gladTwoTech.includes(el) : false
  101. );
  102.  
  103. return bool;
  104. }
  105.  
  106. function arrCreation() {
  107. let gladObj = fights(input);
  108.  
  109. let arr = [];
  110.  
  111. let arr2 = [];
  112.  
  113. for (let key in gladObj) {
  114. arr2 = [];
  115.  
  116. for (let key2 in gladObj[key]) {
  117. arr2.push([key2, gladObj[key][key2]]);
  118. }
  119.  
  120. arr.push([key, arr2]);
  121. }
  122.  
  123. return arr;
  124. }
  125.  
  126. function sorting() {
  127. let arr = arrCreation();
  128.  
  129. arr.sort((a, b) =>
  130. b[1][b[1].length - 1][1] === a[1][a[1].length - 1][1]
  131. ? a[0].localeCompare(b[0])
  132. : b[1][b[1].length - 1][1] - a[1][a[1].length - 1][1]
  133. );
  134.  
  135. for (let el of arr) {
  136. el[1].sort((a, b) =>
  137. a[1] === b[1] ? a[0].localeCompare(b[0]) : b[1] - a[1]
  138. );
  139. }
  140.  
  141. return arr;
  142. }
  143.  
  144. function additionalSorting() {
  145. let arr = sorting();
  146.  
  147. for (let el of arr) {
  148. for (let el2 of el[1]) {
  149. if (el2[0] === "Total" && el[1].indexOf(el2) !== 0) {
  150. el[1].splice(el[1].indexOf(el2), 1);
  151.  
  152. el[1].unshift(el2);
  153. }
  154. }
  155. }
  156.  
  157. return arr;
  158. }
  159. }
  160.  
  161. solve([
  162. "Peter -> Duck -> 400",
  163.  
  164. "Julius -> Shield -> 150",
  165.  
  166. "Gladius -> Heal -> 200",
  167.  
  168. "Gladius -> Support -> 250",
  169.  
  170. "Gladius -> Shield -> 250",
  171.  
  172. "Peter vs Gladius",
  173.  
  174. "Gladius vs Julius",
  175.  
  176. "Gladius vs Maximilian",
  177.  
  178. "Ave Cesar",
  179. ]);
  180.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement