# Armies

Apr 15th, 2021
477
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. function solve(arr) {
3.
4.   for (let info of arr) {
5.     if (info.includes('arrives')) {
6.       const leader = info.substring(0, info.indexOf('arrive') - 1);
8.     } else if (info.includes('defeated')) {
9.       const leader = info.substring(0, info.indexOf('defeated') - 1);
11.     } else if (info.includes(': ')) {
12.       const leader = info.substring(0, info.indexOf(':'));
13.       const army = info.substring(info.indexOf(':') + 2, info.indexOf(','));
14.       let armyCount = Number(info.substring(info.lastIndexOf(' ')),);
16.     } else if (info.includes('+')) {
17.       const army = info.substring(0, info.indexOf(' +') );
18.       const newCount = Number(info.substring(info.indexOf('+') + 1,));
19.       if (leaders.filter(x => x[1].has(army)).length > 0) {
20.         let armyToChange = leaders.filter(x => x[1].has(army))[0][1];
21.         armyToChange.set(army, armyToChange.get(army) + newCount);
22.       }
23.     }
24.   }
25.
26.   for (let leader of leaders.sort((a, b) => [...b[1].values()].reduce((a, b) => a + b) - [...a[1].values()].reduce((a, b) => a + b))) {
28.     [...leader[1].entries()].sort((a, b) => b[1] - a[1]).forEach(x => console.log(`>>> \${x[0]} - \${x[1]}`));
29.   }
30. }
RAW Paste Data