Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input){
- let pirates = {};
- let command = input.shift().split('||');
- while(command[0] !== "Sail"){
- let town = command[0];
- let population = Number(command[1])
- let gold = Number(command[2]);
- // console.log(town)
- if(!pirates.hasOwnProperty(town)){
- pirates[town] = {population, gold}
- } else {
- pirates[town].population += population;
- pirates[town].gold += gold
- }
- command = input.shift().split('||');
- }
- // console.log(pirates)
- for(let i = 0; i < input.length; i++){
- let [command, city, arg2, arg3] = input[i].split('=>');
- // console.log(command)
- switch(command){
- case "Plunder":
- let people = Number(arg2);
- let treasure = Number(arg3);
- pirates[city].population -= people;
- pirates[city].gold += treasure;
- console.log((`${city} plundered! ${treasure} gold stolen, ${people} citizens killed.`))
- if(pirates[city].population <= 0 || pirates[city].gold <= 0){
- console.log(`${city} has been wiped off the map!`)
- delete pirates[city];
- }
- break;
- case "Prosper":
- let prosper = Number(arg2);
- if(prosper >= 0){
- pirates[city].gold += prosper;
- console.log(`${prosper} gold added to the city treasury. ${city} now has ${pirates[city].gold} gold.`)
- } else {
- console.log(`Gold added cannot be a negative number!`)
- }
- break;
- case "End":
- break;
- }
- }
- let sorted = Object.entries(pirates).sort((a,b) => b[1].gold - a[1].gold || a[0].localeCompare(b[0]));
- // console.log(sorted)
- if(sorted.length > 0){
- console.log(`Ahoy, Captain! There are ${sorted.length} wealthy settlements to go to:`);
- for(let kvp of sorted){
- console.log(`${kvp[0]} -> Population: ${kvp[1].population} citizens, Gold: ${kvp[1].gold} kg`);
- }
- } else {
- console.log(`Ahoy, Captain! All targets have been plundered and destroyed!`)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment