Lulunga

Arena tier alternative Associative Arrays

Jul 13th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     input.pop();
  3.     let list = {};
  4.  
  5.     for (let line of input) {
  6.  
  7.         if (line.includes('vs')) {
  8.             let [firstGladiator, secondGladiator] = line.split(' vs ');
  9.             if ((firstGladiator in list) && (secondGladiator in list)) {
  10.                 let firstGladiatorTechniques = list[firstGladiator];
  11.                 let secondGladiatorTechniques = list[secondGladiator];
  12.  
  13.                 for (const key in firstGladiatorTechniques) {
  14.                     if (firstGladiatorTechniques.hasOwnProperty(key)) {
  15.                         if ((key in secondGladiatorTechniques) && (key !== 'totalSkill')) {
  16.                             if (firstGladiatorTechniques.totalSkill > secondGladiatorTechniques.totalSkill) {
  17.                                 delete list[secondGladiator];
  18.                                 break;
  19.                             }else {
  20.                                 delete list[firstGladiator];
  21.                                 break
  22.                             }
  23.                         }
  24.                     }
  25.                 }
  26.             }
  27.         } else {
  28.             let [gladiator, technique, skill] = line.split(' -> ');
  29.             skill = Number(skill)
  30.             if (!(gladiator in list)) {
  31.                 list[gladiator] = { [technique]: skill };
  32.                 list[gladiator].totalSkill = skill
  33.             } else {
  34.                 if (!(technique in list[gladiator])) {
  35.                     list[gladiator][technique] = skill;
  36.                     list[gladiator].totalSkill += skill;
  37.                 } else {
  38.                     if (list[gladiator][technique] < skill) {
  39.                         list[gladiator].totalSkill = list[gladiator][technique] - skill;
  40.                         list[gladiator][technique] = skill;
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.     }
  46.     let entries = Object.entries(list)
  47.         .sort((a, b) => a[0].localeCompare(b[0]))
  48.         .sort((a, b) => b[1].totalSkill - a[1].totalSkill);
  49.      
  50.     for (const line of entries) {
  51.         console.log(`${line[0]}: ${line[1].totalSkill} skill`);
  52.         delete line[1].totalSkill;
  53.         Object.entries(line[1])
  54.             .sort((a, b) => a[0].localeCompare(b[0]))
  55.             .sort((a, b) => b[1] - a[1])
  56.             .forEach(e => console.log(`- ${e[0]} <!> ${e[1]}`));
  57.     }
  58. }
Add Comment
Please, Sign In to add comment