Advertisement
Guest User

solveMOBAChallenger

a guest
Apr 6th, 2019
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solveMOBAChallenger(input) {
  2.     let index = 0
  3.     let command = input[index++]
  4.     let playersMap = new Map()
  5.  
  6.     while (command !== 'Season end') {
  7.  
  8.         if (!command.includes('vs')) {
  9.             command = command.split(' -> ')
  10.             let player = command[0]
  11.             let position = command[1]
  12.             let skill = Number(command[2])
  13.  
  14.             if (playersMap.has(player)) {
  15.                 let positionsAndSkills = playersMap.get(player)
  16.                 let currtotalSkill = positionsAndSkills.totalSkill
  17.  
  18.                 if (positionsAndSkills.hasOwnProperty(position)) {
  19.                     let currPosSkills = positionsAndSkills[position]
  20.  
  21.                     // update his skill, only if the current position skill is lower than the new value
  22.                     if (currPosSkills < skill) {
  23.                         positionsAndSkills[position] = skill
  24.                         currtotalSkill += (skill - currPosSkills)  // every time increase the totalSkill
  25.                     }
  26.  
  27.                 } else {
  28.                     positionsAndSkills[position] = skill
  29.                     currtotalSkill += skill  // every time increase the totalSkill
  30.                 }
  31.  
  32.                 positionsAndSkills.totalSkill = currtotalSkill
  33.                 playersMap.set(player, positionsAndSkills)
  34.  
  35.             } else {
  36.                 let positionAndSkill = { [position]: skill, totalSkill: skill }
  37.                 playersMap.set(player, positionAndSkill)
  38.             }
  39.  
  40.         } else {
  41.             command = command.split(' vs ')
  42.             let firstPlayer = command[0]
  43.             let secondPlayer = command[1]
  44.  
  45.             if (playersMap.has(firstPlayer) && playersMap.has(secondPlayer)) {
  46.                 let firstPlayerPos = playersMap.get(firstPlayer)
  47.                 let firstPlayerPosArr = Object.entries(firstPlayerPos)
  48.                 let secondPlayerPos = playersMap.get(secondPlayer)
  49.                 let secondPlayerPosArr = Object.entries(secondPlayerPos)
  50.                 let removedPlayer = ''
  51.  
  52.                 for (let firstEl of firstPlayerPosArr) {
  53.                     for (let secondEl of secondPlayerPosArr) {
  54.  
  55.                         if (firstEl[0] !== 'totalSkill' && secondEl[0] !== 'totalSkill' && firstEl[0] === secondEl[0]) {
  56.                             let firstPlTotal = playersMap.get(firstPlayer).totalSkill
  57.                             let secondPlTotal = playersMap.get(secondPlayer).totalSkill
  58.  
  59.                             if (firstPlTotal > secondPlTotal) {
  60.                                 removedPlayer = secondPlayer
  61.  
  62.                             } else if (secondPlTotal > firstPlTotal) {
  63.                                 removedPlayer = firstPlayer
  64.                             }
  65.                         }
  66.                     }
  67.                 }
  68.                 playersMap.delete(removedPlayer)
  69.             }
  70.         }
  71.  
  72.         command = input[index++]
  73.     }
  74.  
  75.     // Order the players by total skill in desecending order, then ordered by player name in ascending order
  76.     let sortedPlayers = [...playersMap.entries()]
  77.         .sort((a, b) => b[1].totalSkill - a[1].totalSkill || a[0].localeCompare(b[0]))
  78.  
  79.     let output = ''
  80.     // For each player print their position and skill, ordered desecending by skill, then ordered by position name in ascending order.
  81.     for (let player of sortedPlayers) {
  82.         let playerInfo = Object.entries(player[1])
  83.         output += `${player[0]}: ${player[1].totalSkill} skill\n`
  84.  
  85.         playerInfo = playerInfo.filter(posSkill => posSkill[0] !== 'totalSkill')
  86.             .sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
  87.             .forEach(posSkill => output += `- ${posSkill[0]} <::> ${posSkill[1]}\n`)
  88.     }
  89.  
  90.     console.log(output);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement