ErolKZ

Untitled

Nov 17th, 2021
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1.  
  2. function solve(arr) {
  3. let pool = {};
  4.  
  5. let nestObj = {};
  6.  
  7. let i = 0;
  8.  
  9. let skillsTotal = 0;
  10.  
  11. let skillArr = [];
  12.  
  13. let sortingArr = [];
  14.  
  15. while (arr[i] !== "Ave Cesar") {
  16. let arr2 = arr[i].split(" ");
  17.  
  18. if (arr2.length > 3) {
  19. let curEl = arr[i].split(" -> ");
  20.  
  21. if (!pool.hasOwnProperty(curEl[0])) {
  22. nestObj[curEl[1]] = curEl[2];
  23.  
  24. pool[curEl[0]] = nestObj;
  25.  
  26. nestObj = {};
  27. } else {
  28. if (pool[curEl[0]].hasOwnProperty(curEl[1])) {
  29. if (pool[curEl[0]][curEl[1]] < curEl[2]) {
  30. pool[curEl[0]][curEl[1]] = curEl[2];
  31. }
  32. } else {
  33. pool[curEl[0]][curEl[1]] = curEl[2];
  34. }
  35. }
  36. } else {
  37. let curEl = arr[i].split(" vs ");
  38.  
  39. let gladOne = curEl[0];
  40.  
  41. let gladTwo = curEl[1];
  42.  
  43. if (!pool.hasOwnProperty(gladOne) || !pool.hasOwnProperty(gladTwo)) {
  44. i++;
  45.  
  46. continue;
  47. }
  48.  
  49. let tehArr = Object.keys(pool[gladOne]);
  50.  
  51. let tehArr2 = Object.keys(pool[gladTwo]);
  52.  
  53. if (tehArr.some((el) => tehArr2.includes(el))) {
  54. let losingSide = [];
  55.  
  56. for (let el of curEl) {
  57. for (let key in pool[el]) {
  58. losingSide.push([el, pool[el][key]]);
  59. }
  60. }
  61.  
  62. losingSide.sort((a, b) => a[1] - b[1]);
  63.  
  64. losingSide.pop();
  65.  
  66. losingSide = losingSide.flat();
  67.  
  68. losingSide = losingSide[0];
  69.  
  70. delete pool[losingSide];
  71. }
  72. }
  73.  
  74. i++;
  75. }
  76.  
  77. for (let key in pool) {
  78. skillArr = Object.entries(pool[key])
  79. .flat()
  80. .filter((el) => (!isNaN(el) ? el : false));
  81.  
  82. skillsTotal = skillArr.reduce((acc, el) => acc + Number(el), 0);
  83.  
  84. pool[key].skill = skillsTotal;
  85. }
  86.  
  87. for (let key in pool) {
  88. sortingArr.push([key, pool[key]]);
  89. }
  90.  
  91. sortingArr.sort((a, b) =>
  92. b[1]["skill"] !== a[1]["skill"]
  93. ? b[1]["skill"] - a[1]["skill"]
  94. : a[0].localeCompare(b[0])
  95. );
  96.  
  97. for (let i = 0; i < sortingArr.length; i++) {
  98. let subsection = Object.entries(sortingArr[i][1]).sort((a, b) =>
  99. b[1] !== a[1] ? b[1] - a[1] : a[0].localeCompare(b[0])
  100. );
  101.  
  102. sortingArr[i].splice(1, 1, subsection);
  103. }
  104.  
  105. for (let i = 0; i < sortingArr.length; i++) {
  106. console.log(`${sortingArr[i][0]}: ${sortingArr[i][1][0][1]} skill`);
  107.  
  108. sortingArr[i][1].forEach((el) =>
  109. el[0] === "skill"
  110. ? sortingArr[i][1].splice(sortingArr[i][1].indexOf(el), 1)
  111. : false
  112. );
  113.  
  114. sortingArr[i].shift();
  115.  
  116. sortingArr[i] = sortingArr[i].flat();
  117.  
  118. for (let j = 0; j < sortingArr[i].length; j++) {
  119. console.log(`- ${sortingArr[i][j][0]} <!> ${sortingArr[i][j][1]}`);
  120. }
  121. }
  122.  
  123. // console.log(sortingArr);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment