Advertisement
KrasimirKosturkov

Untitled

Apr 5th, 2020
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.    let sities = {};
  3.    let count = 0;
  4.    for(let sity of input){
  5.     let [town, population, gold] = sity.split('||')
  6.        if(town === 'Sail'){
  7.            break;
  8.        }
  9.        if(!sities.hasOwnProperty(town)){
  10.            sities[town] = [];
  11.            sities[town].push(+(population), +(gold));
  12.            count++;
  13.        }else{
  14.            sities[town][0] += +(population);
  15.            sities[town][1] += +(gold);
  16.        }
  17.    }
  18.    for(let command of input){
  19.        if(command === 'End'){
  20.            break;
  21.        }
  22.        let [plunderOrProsper, town, peopleOrGold, gold] = command.split('=>');
  23.        if(plunderOrProsper === 'Plunder'){
  24.            console.log(`${town} plundered! ${gold} gold stolen, ${peopleOrGold} citizens killed.`);
  25.            sities[town][0] -= +(peopleOrGold);
  26.            sities[town][1] -= +(gold);
  27.            if(0 >= sities[town][0] || sities[town][1] <= 0){
  28.                delete sities[town];
  29.                console.log(`${town} has been wiped off the map!`);
  30.                count--;
  31.            }
  32.        }else if(plunderOrProsper === 'Prosper'){
  33.            if(+(peopleOrGold) < 0){
  34.                console.log('Gold added cannot be a negative number!');
  35.            }else{
  36.                sities[town][1] += +(peopleOrGold);
  37.                console.log(`${peopleOrGold} gold added to the city treasury. ${town} now has ${sities[town][1]} gold.`);
  38.            }
  39.        }
  40.    }
  41.    let result = Object.entries(sities);
  42.    result.sort((a, b)=> b[1][1] - a[1][1] || a[0].localeCompare(b[0]));
  43.    if(result === []){
  44.        console.log('Ahoy, Captain! All targets have been plundered and destroyed!');
  45.        return;
  46.    }
  47.    console.log(`Ahoy, Captain! There are ${count} wealthy settlements to go to:`);
  48.     for(let item of result){
  49.         console.log(`${item[0]} -> Population: ${item[1][0]} citizens, Gold: ${item[1][1]} kg`)
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement