desito07

P!rates

Aug 3rd, 2020
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.    
  3.     let pirates = {};
  4.  
  5.         let command = input.shift().split('||');
  6.             while(command[0] !== "Sail"){            
  7.             let town = command[0];
  8.             let population = Number(command[1])
  9.             let gold = Number(command[2]);
  10.            
  11.             // console.log(town)
  12.                 if(!pirates.hasOwnProperty(town)){      
  13.                     pirates[town] = {population, gold}
  14.                 } else {
  15.                     pirates[town].population += population;
  16.                     pirates[town].gold += gold
  17.                 }
  18.                 command = input.shift().split('||');
  19.             }        
  20.         // console.log(pirates)
  21.         for(let i = 0; i < input.length; i++){
  22.             let [command, city, arg2, arg3] = input[i].split('=>');
  23.             // console.log(command)
  24.             switch(command){
  25.                 case "Plunder":                    
  26.                     let people = Number(arg2);
  27.                     let treasure = Number(arg3);
  28.                     pirates[city].population -= people;
  29.                     pirates[city].gold += treasure;
  30.                     console.log((`${city} plundered! ${treasure} gold stolen, ${people} citizens killed.`))
  31.                     if(pirates[city].population <= 0 || pirates[city].gold <= 0){
  32.                         console.log(`${city} has been wiped off the map!`)
  33.                         delete pirates[city];
  34.                     }
  35.                     break;
  36.                 case "Prosper":
  37.                     let prosper = Number(arg2);
  38.                     if(prosper >= 0){
  39.                         pirates[city].gold += prosper;
  40.                         console.log(`${prosper} gold added to the city treasury. ${city} now has ${pirates[city].gold} gold.`)
  41.                     } else {
  42.                         console.log(`Gold added cannot be a negative number!`)
  43.                     }
  44.                     break;
  45.                 case "End":
  46.                     break;
  47.             }
  48.         }
  49.  
  50.     let sorted = Object.entries(pirates).sort((a,b) => b[1].gold - a[1].gold || a[0].localeCompare(b[0]));
  51.     // console.log(sorted)
  52.  
  53.     if(sorted.length > 0){
  54.         console.log(`Ahoy, Captain! There are ${sorted.length} wealthy settlements to go to:`);
  55.         for(let kvp of sorted){
  56.             console.log(`${kvp[0]} -> Population: ${kvp[1].population} citizens, Gold: ${kvp[1].gold} kg`);
  57.         }  
  58.     } else {
  59.         console.log(`Ahoy, Captain! All targets have been plundered and destroyed!`)
  60.     }    
  61. }
Advertisement
Add Comment
Please, Sign In to add comment