AreWe

Pirates

Jul 27th, 2020
1,626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pirates = (input = []) => {
  2.     return [
  3.         (cities = input
  4.             .slice(0, input.indexOf('Sail'))
  5.             .map((city) => city.split('||'))
  6.             .map((city) => [city[0], ...city.slice(1).map(Number)])
  7.             .reduce(
  8.                 (cities, [name, population, gold]) =>
  9.                     Object.assign(cities, {
  10.                         [name]: {
  11.                             population: population + (cities[name] ? cities[name].population : 0),
  12.                             gold: gold + (cities[name] ? cities[name].gold : 0),
  13.                         },
  14.                     }),
  15.                 {},
  16.             )),
  17.         (commands = input
  18.             .slice(input.indexOf('Sail') + 1, input.indexOf('End'))
  19.             .map((cmd) => cmd.split('=>'))
  20.             .map((cmd) => [...cmd.slice(0, 2), ...cmd.slice(2).map(Number)])),
  21.     ][1]
  22.         .reduce((output, cmd) => {
  23.             if (cmd[0] === 'Plunder') {
  24.                 const city = cities[cmd[1]];
  25.                 [city.population, city.gold] = [city.population - cmd[2], city.gold - cmd[3]];
  26.                 output.push(`${cmd[1]} plundered! ${cmd[3]} gold stolen, ${cmd[2]} citizens killed.`);
  27.                 if (!city.population || !city.gold) {
  28.                     delete cities[cmd[1]];
  29.                     output.push(`${cmd[1]} has been wiped off the map!`);
  30.                 }
  31.             } else {
  32.                 // 'Prosper'
  33.                 if (cmd[2] < 0) {
  34.                     output.push('Gold added cannot be a negative number!');
  35.                 } else {
  36.                     cities[cmd[1]].gold += cmd[2];
  37.                     output.push(
  38.                         `${cmd[2]} gold added to the city treasury. ${cmd[1]} now has ${cities[cmd[1]].gold} gold.`,
  39.                     );
  40.                 }
  41.             }
  42.  
  43.             return output;
  44.         }, [])
  45.         .concat(
  46.             !Object.keys(cities).length
  47.                 ? '\nAhoy, Captain! All targets have been plundered and destroyed!'
  48.                 : [`Ahoy, Captain! There are ${Object.keys(cities).length} wealthy settlements to go to:`].concat(
  49.                       Object.entries(cities)
  50.                           .sort((a, b) => b[1].gold - a[1].gold || a[0].localeCompare(b[0]))
  51.                           .map(
  52.                               ([name, { population, gold }]) =>
  53.                                   `${name} -> Population: ${population} citizens, Gold: ${gold} kg`,
  54.                           ),
  55.                   ),
  56.         )
  57.         .join('\n');
  58. };
Advertisement
Add Comment
Please, Sign In to add comment