maymunskoa

09. Arena Tier (Assoc Arrays)

Mar 7th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.    
  3. class Gladiator{
  4.     constructor(name){
  5.         this.name = name;
  6.         this.technique = [];
  7.         this.skill = [];
  8.     }
  9.  
  10.     updateSet(techVal, skillVal){
  11.         let index = this.technique.indexOf(techVal);
  12.         if(index === -1){
  13.             this.technique.push(techVal);
  14.             this.skill.push(Number(skillVal));
  15.         }else{
  16.             if(this.skill[index] < Number(skillVal)){
  17.                 this.skill[index] = Number(skillVal);
  18.             }
  19.         }
  20.     }
  21.  
  22.     getScore(){
  23.         return this.skill.reduce((a,b)=>{
  24.             return a + b;
  25.         });
  26.     }
  27.  
  28.  
  29.     sortBoth(){
  30.         let helpMap = new Map();
  31.         for (const one of this.technique) {
  32.             let index = this.technique.indexOf(one);
  33.             helpMap.set(one,this.skill[index]);
  34.         }
  35.         let helpertech = [];
  36.         let helperSkill = [];
  37.         Array.from(helpMap).sort((a,b)=>{
  38.             if(a[1] === b[1]){
  39.                 return a[0].localeCompare(b[0])
  40.             }else{
  41.                 return b[1] - a[1];
  42.             }
  43.            
  44.         }).forEach(pair=>{
  45.             helpertech.push(pair[0]);
  46.             helperSkill.push(pair[1]);
  47.         });
  48.  
  49.         this.technique = [];
  50.         this.technique.push(...helpertech);
  51.         this.skill = [];
  52.         this.skill.push(...helperSkill);
  53.     }
  54.  
  55. }
  56.  
  57.  
  58.  
  59. let pool = [];
  60. let nameSet = [];
  61.  
  62.  
  63. for (const line of input) {
  64.         if(line === 'Ave Cesar'){
  65.             break;
  66.         }
  67.  
  68.  
  69.         if(line.match(' -> ')){
  70.             let [name, technique, skill] = line.split(' -> ');
  71.            
  72.             let index = nameSet.indexOf(name);
  73.             if(index === -1){
  74.                 let obj = new Gladiator(name);
  75.                 obj.updateSet(technique,skill);
  76.                 pool.push(obj);
  77.                 nameSet.push(name);
  78.             }else{
  79.                 let current = pool[index];
  80.                 current.updateSet(technique,skill);
  81.                 pool[index] = current;
  82.             }
  83.         }else{
  84.             let [nameOne, nameTwo] = line.split(' vs ');
  85.             let indexOne = nameSet.indexOf(nameOne);
  86.             let indexTwo = nameSet.indexOf(nameTwo);
  87.             if(indexOne > -1 && indexTwo > -1){
  88.                 let p1 = pool[indexOne];
  89.                 let p2 = pool[indexTwo];
  90.                 let isFight = false;
  91.                 for (const one of p1.technique) {
  92.                     if(p2.technique.includes(one)){
  93.                         isFight = true;
  94.                         break;
  95.                     }
  96.                 }
  97.                 if(isFight === true){
  98.                     let p1Score = p1.getScore();
  99.                     let p2Score = p2.getScore();
  100.  
  101.                     if(p1Score > p2Score){
  102.                         pool.splice(indexTwo,1);
  103.                         nameSet.splice(indexTwo,1);
  104.                     }else if(p2Score > p1Score){
  105.                         pool.splice(indexOne,1);
  106.                         nameSet.splice(indexOne,1);
  107.                     }
  108.                 }
  109.             }
  110.         }
  111. }
  112.  
  113.  
  114. pool.sort((a,b)=>b.getScore()-a.getScore());
  115.  
  116. pool.forEach(object=>{
  117.     object.sortBoth();
  118.     console.log(`${object.name}: ${object.getScore()} skill`);
  119.     object.technique.forEach((item,index)=>{
  120.         console.log(`- ${item} <!> ${object.skill[index]}`)
  121.     })
  122.    
  123.    
  124.    
  125. })
  126. }
Advertisement
Add Comment
Please, Sign In to add comment