Advertisement
Guest User

solveMOBAChallenger

a guest
Apr 6th, 2019
196
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.  
  51.                 for (let firstEl of firstPlayerPosArr) {
  52.                     for (let secondEl of secondPlayerPosArr) {
  53.  
  54.                         if (firstEl[0] !== 'totalSkill' && secondEl[0] !== 'totalSkill' && firstEl[0] === secondEl[0]) {
  55.                             let firstPlTotal = playersMap.get(firstPlayer).totalSkill
  56.                             let secondPlTotal = playersMap.get(secondPlayer).totalSkill
  57.  
  58.                             if (firstPlTotal > secondPlTotal) {
  59.                                 playersMap.delete(secondPlayer)
  60.  
  61.                             } else if (secondPlTotal > firstPlTotal) {
  62.                                 playersMap.delete(firstPlayer)
  63.                             }
  64.                         }
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.  
  70.         command = input[index++]
  71.     }
  72.  
  73.     // Order the players by total skill in desecending order, then ordered by player name in ascending order
  74.     let sortedPlayers = [...playersMap.entries()]
  75.         .sort((a, b) => b[1].totalSkill - a[1].totalSkill || a[0].localeCompare(b[0]))
  76.  
  77.     let output = ''
  78.     // For each player print their position and skill, ordered desecending by skill, then ordered by position name in ascending order.
  79.     for (let player of sortedPlayers) {
  80.         let playerInfo = Object.entries(player[1])
  81.         output += `${player[0]}: ${player[1].totalSkill} skill\n`
  82.  
  83.         playerInfo = playerInfo.filter(posSkill => posSkill[0] !== 'totalSkill')
  84.             .sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
  85.             .forEach(posSkill => output += `- ${posSkill[0]} <::> ${posSkill[1]}\n`)
  86.     }
  87.  
  88.     console.log(output);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement