Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(args) {
- let sailInputLine = args.shift();
- const cities = {};
- while (sailInputLine !== 'Sail') {
- let [cityName, population, gold] = sailInputLine.split('||');
- population = Number(population);
- gold = Number(gold);
- if (!Object.prototype.hasOwnProperty.call(cities, cityName)) {
- cities[cityName] = { population, gold };
- } else {
- cities[cityName].population += population;
- cities[cityName].gold += gold;
- }
- sailInputLine = args.shift();
- }
- let eventInputLine = args.shift();
- while (eventInputLine !== 'End') {
- let [event, town, arg1, arg2] = eventInputLine.split('=>');
- arg1 = Number(arg1);
- arg2 = Number(arg2);
- switch (event) {
- case 'Plunder':
- console.log(`${town} plundered! ${arg2} gold stolen, ${arg1} citizens killed.`);
- cities[town].population -= arg1;
- cities[town].gold -= arg2;
- if (cities[town].population === 0 || cities[town].gold === 0) {
- delete cities[town];
- console.log(`${town} has been wiped off the map!`);
- }
- break;
- case 'Prosper':
- if (arg1 > 0) {
- cities[town].gold += arg1;
- console.log(`${arg1} gold added to the city treasury. ${town} now has ${cities[town].gold} gold.`);
- } else {
- console.log('Gold added cannot be a negative number!');
- }
- break;
- default:
- break;
- }
- eventInputLine = args.shift();
- }
- if (Object.keys(cities).length !== 0) {
- console.log(`Ahoy, Captain! There are ${Object.keys(cities).length} wealthy settlements to go to:`);
- Object.entries(cities)
- .sort((a, b) => b[1].gold - a[1].gold || a[0].localeCompare(b[0]))
- .forEach((city) => {
- const [name, props] = city;
- const { population, gold } = props;
- console.log(`${name} -> Population: ${population} citizens, Gold: ${gold} kg`);
- });
- } else {
- console.log('Ahoy, Captain! All targets have been plundered and destroyed!');
- }
- }
Add Comment
Please, Sign In to add comment