maymunskoa

10. Legendary Farming (Assoc Arrays)

Mar 8th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.  
  3. let legendary = {
  4.     shards: 'Shadowmourne',
  5.     fragments:'Valanyr',
  6.     motes:'Dragonwrath',
  7. }
  8.  
  9. let mainResource = {
  10.     shards:0,
  11.     fragments:0,
  12.     motes:0
  13.  
  14. };
  15.  
  16. let regularResource = {};
  17.  
  18.  
  19. function populate(material,quantity){
  20.     let currentQuantity = 0;
  21.     if(!mainResource.hasOwnProperty(material)){
  22.         if(!regularResource.hasOwnProperty(material)){
  23.             regularResource[material] = quantity;
  24.         }else{
  25.             currentQuantity = regularResource[material];
  26.             currentQuantity += quantity;
  27.             regularResource[material] = currentQuantity;
  28.         }
  29.     }else{
  30.         currentQuantity = mainResource[material];
  31.         currentQuantity += quantity;
  32.         mainResource[material] = currentQuantity;
  33.     }
  34. }
  35.  
  36. let incomingResources = input.split(' ');
  37. for(let i = 0; i < incomingResources.length; i+=2){
  38.     let quantity = Number(incomingResources[i]);
  39.     let material = incomingResources[i+1].toLowerCase();
  40.  
  41.     populate(material,quantity);
  42.     if(mainResource[material] >= 250){
  43.         console.log(`${legendary[material]} obtained!`);
  44.         let value = mainResource[material];
  45.         value -=250;
  46.         mainResource[material] = value;
  47.         break;
  48.     }
  49. }
  50.     Object.entries(mainResource).sort((a,b)=>{
  51.         if(a[1] === b[1]){
  52.             return a[0].localeCompare(b[0]);
  53.         }
  54.         return b[1] - a[1];
  55.     }).forEach(pair=>{
  56.         let key = pair[0];
  57.         let val = pair[1];
  58.             console.log(`${key}: ${val}`);
  59.        
  60.     });
  61.     Object.entries(regularResource).sort((a,b)=>{
  62.         return a[0].localeCompare(b[0]);
  63.     }).forEach(pair=>{
  64.         console.log(`${pair[0]}: ${pair[1]}`);
  65.        
  66.     });
  67.    
  68.    
  69. }
Advertisement
Add Comment
Please, Sign In to add comment