Advertisement
bebo231312312321

Untitled

Apr 1st, 2023
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function pirates(input) {
  2.     let townList = {}
  3.     let townInfo = input.splice(0, input.indexOf("Sail")).map(line => {
  4.         let [town, people, gold] = line.split("||").map(x => isNaN(x) ? x : Number(x))
  5.         !townList.hasOwnProperty(town) ? townList[town] = { people, gold }
  6.             : (townList[town].people += people, townList[town].gold += gold);
  7.     })
  8.     let secondLine = input.splice(1, input.indexOf("End") - 1).map(line => {
  9.         let [command, a, b, c] = line.split("=>").map(x => isNaN(x) ? x : Number(x));
  10.         switch (command) { case "Prosper": prosper(a, b); break; case "Plunder": plunder(a, b, c); break; };
  11.     })
  12.     function prosper(a, b) {
  13.         (b < 0) ? console.log(`Gold added cannot be a negative number!`)
  14.             : (townList[a].gold += b, console.log(`${b} gold added to the city treasury. ${a} now has ${townList[a].gold} gold.`));
  15.     }
  16.     function plunder(a, b, c) {
  17.         console.log(`${a} plundered! ${c} gold stolen, ${b} citizens killed.`);
  18.         if (townList.hasOwnProperty(a)) {
  19.             townList[a].people -= b; townList[a].gold -= c
  20.         }
  21.         if (townList[a].people == 0 || townList[a].gold == 0) {
  22.             delete townList[a], console.log(`${a} has been wiped off the map!`);
  23.         }
  24.     }
  25.     if ((Object.keys(townList)).length > 0) {
  26.         console.log(`Ahoy, Captain! There are ${(Object.keys(townList)).length} wealthy settlements to go to:`)
  27.         for (let key in townList) {
  28.             console.log(`${key} -> Population: ${townList[key].people} citizens, Gold: ${townList[key].gold} kg`)
  29.         };
  30.     } else {
  31.         console.log(`Ahoy, Captain! All targets have been plundered and destroyed!`)
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement