Guest User

Untitled

a guest
Sep 25th, 2019
1,139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. function solve(arr) {
  2. let list = {}
  3. for (let el of arr) {
  4. if (el === 'Ave Cesar') {
  5. break
  6. }
  7. else if (el.includes(' -> ')) {
  8. add(el)
  9. }
  10. else if (el.includes(' vs ')) {
  11. battle(el)
  12. }
  13. }
  14. let tier = Object.entries(list)
  15. let array = []
  16. for (let elem of tier) {
  17. let name = elem[0]
  18. let pow = Object.entries(elem[1])
  19. let sum = pow.map(a => a[1]).reduce((a, b) => a + b)
  20. array.push([name, pow, sum])
  21. }
  22. array.sort((a, b) => b[2] - a[2] || a[0].localeCompare(b[0]))
  23. for (let part of array) {
  24. console.log(`${part[0]}: ${part[2]} skill`);
  25. part[1]
  26. .sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
  27. .map(x => console.log(`- ${x[0]} <!> ${x[1]}`))
  28. }
  29.  
  30. function add(el) {
  31. let [gladiator, skill, power] = el.split(' -> ')
  32. power = Number(power)
  33. if (!list.hasOwnProperty(gladiator)) {
  34. list[gladiator] = {}
  35. list[gladiator][skill] = power
  36. }
  37. else {
  38. if (!list[gladiator].hasOwnProperty(skill)) {
  39. list[gladiator][skill] = power
  40. }
  41. else {
  42. let oldPow = list[gladiator][skill]
  43. if (power > oldPow) {
  44. list[gladiator][skill] = power
  45. }
  46. }
  47. }
  48. }
  49. function battle(el) {
  50. let [gladiatorA, gladiatorB] = el.split(' vs ')
  51. if (list.hasOwnProperty(gladiatorA) && list.hasOwnProperty(gladiatorB)) {
  52. let skillA = list[gladiatorA]
  53. let skillB = list[gladiatorB]
  54. for (let elA in skillA) {
  55. for (let elB in skillB) {
  56. if (elA === elB) {
  57. if (skillA[elA] > skillB[elB]) {
  58. delete list[gladiatorB]
  59. }
  60. else if (skillA[elA] < skillB[elB]) {
  61. delete list[gladiatorA]
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment