Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(params) {
- let armies = {};
- let armyLeaders = [];
- for (let param of params) {
- if (param.includes("arrives")) {
- let indexOfArrives = param.indexOf("arrives");
- let leader = param.slice(0, indexOfArrives).trim();
- if (!armies.hasOwnProperty(leader)) {
- armies[leader] = {
- armiesName: {},
- totalArmyCount: 0
- };
- }
- armyLeaders.push(leader);
- } else if (param.includes("defeated")) {
- let indexOfDefeated = param.indexOf("defeated");
- let leader = param.slice(0, indexOfDefeated).trim();
- let indexOfLeader = armyLeaders.indexOf(leader);
- if (armies.hasOwnProperty(leader)) {
- delete armies[leader];
- armyLeaders.splice(indexOfLeader, 1);
- }
- } else if (param.includes(":")) {
- let [leader, army] = param.split(": ");
- let [armyName, armyCount] = army.split(", ");
- if (armies.hasOwnProperty(leader)) {
- if (!armies[leader].armiesName.hasOwnProperty(armyName)) {
- armies[leader].armiesName[armyName] = Number(armyCount);
- armies[leader].totalArmyCount += Number(armyCount);
- }
- }
- } else if (param.includes("+")) {
- let [armyName, armyCount] = param.split(" + ");
- armyLeaders.forEach(leader => {
- for (let army in armies[leader]) {
- if (armies[leader][army].hasOwnProperty(armyName)) {
- armies[leader].armiesName[armyName] += Number(armyCount);
- armies[leader].totalArmyCount += Number(armyCount);
- }
- }
- });
- }
- }
- Object.entries(armies)
- .sort((a, b) => b[1].totalArmyCount - a[1].totalArmyCount)
- .forEach(army => {
- console.log(`${army[0]}: ${army[1].totalArmyCount}`);
- Object.values(army[1]).forEach(armyInfo =>
- Object.entries(armyInfo).forEach(a =>
- console.log(`>>> ${a[0]} - ${a[1]}`)
- )
- );
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement