• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Jan 26th, 2020 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. function solve(arr) {
2.     let kingdomEntries = arr[0];
3.     let battleEntries = arr[1];
4.
5.     kingdoms = []
6.
7.     for (let i = 0; i < kingdomEntries.length; i++) {
8.         const entry = kingdomEntries[i];
9.         let kingdomName = entry.kingdom;
10.         let generalName = entry.general;
11.         let armyCount = entry.army;
12.
13.         if (kingdoms.filter(k => k.name === kingdomName).length > 0) {
14.             let kingdom = kingdoms.filter(k => k.name === kingdomName)[0];
15.             if (kingdom.generals.filter(g => g.name === generalName).length > 0){
16.                 let general = kingdom.generals.filter(g => g.name === generalName)[0];
17.                 general.army += armyCount;
18.             } else {
19.                 kingdom.generals.push({
20.                     name: generalName,
21.                     army: armyCount,
22.                     wins: 0,
23.                     losses: 0,
24.                 })
25.             }
26.         } else {
27.             kingdoms.push({
28.                 name: kingdomName,
29.                 generals: [{
30.                     name: generalName,
31.                     army: armyCount,
32.                     wins: 0,
33.                     losses: 0,
34.                 }],
35.                 totalWins: 0,
36.                 totalLosses: 0,
37.             })
38.         }
39.     }
40.
41.     for (let i = 0; i < battleEntries.length; i++) {
42.         const entry = battleEntries[i];
43.         let attKingdomName = entry[0];
44.         let attGeneralName = entry[1];
45.         let defKingdomName = entry[2];
46.         let defGeneralName = entry[3];
47.
48.         let attGeneral;
49.         let defGeneral;
50.
51.         if(attKingdomName === defKingdomName){
52.             continue;
53.         }
54.
55.         let attKingdom = kingdoms.filter(k => k.name === attKingdomName)[0];
56.         let defKingdom = kingdoms.filter(k => k.name === defKingdomName)[0];
57.
58.         if(kingdoms.filter(k => k.name === attKingdomName)[0].generals.filter(g => g.name === attGeneralName).length > 0){
59.             attGeneral = kingdoms.filter(k => k.name === attKingdomName)[0].generals.filter(g => g.name === attGeneralName)[0];
60.         }
61.
62.         if(kingdoms.filter(k => k.name === defKingdomName)[0].generals.filter(g => g.name === defGeneralName).length > 0){
63.             defGeneral = kingdoms.filter(k => k.name === defKingdomName)[0].generals.filter(g => g.name === defGeneralName)[0];
64.         }
65.
66.         if(attGeneral.army === defGeneral.army){
67.             continue;
68.         }
69.
70.         let winner = attGeneral;
71.         let loser = defGeneral;
72.         let winnerKingdom = attKingdom;
73.         let losingKingdom = defKingdom;
74.         if(attGeneral.army < defGeneral.army){
75.             winner = defGeneral;
76.             winnerKingdom = defKingdom;
77.             loser = attGeneral;
78.             losingKingdom = attKingdom;
79.         }
80.
81.         loser.army = Math.floor(loser.army * 0.9);
82.         loser.losses += 1;
83.         losingKingdom.totalLosses += 1;
84.         winner.army = Math.floor(winner.army * 1.1);
85.         winner.wins += 1;
86.         winnerKingdom.totalWins += 1;
87.     }
88.
89.     kingdoms.sort(function(a, b){
90.         if(a.totalWins > b.totalWins) {return -1; }
91.         if(a.totalWins < b.totalWins) {return 1; }
92.         else{
93.             if(a.totalLosses < b.totalLosses) {return -1; }
94.             if(a.totalLosses > b.totalLosses) {return 1; }
95.             else{
96.                 a.name.localeCompare(b.name);
97.             }
98.         }
99.     })
100.
101.     let winningKingdom = kingdoms[0];
102.     winningKingdom.generals.sort(function(a, b){
103.         return b.army - a.army;
104.     });
105.
106.     console.log(`Winner: \${winningKingdom.name}`);
107.     winningKingdom.generals.forEach(general => {
108.         console.log(`/\general: \${general.name}`);
109.         console.log(`---army: \${general.army}`);
110.         console.log(`---wins: \${general.wins}`);
111.         console.log(`---losses: \${general.losses}`);
112.     });
113. }
114.
115. solve([[{ kingdom: "Maiden Way", general: "Merek", army: 5000 },
116. { kingdom: "Stonegate", general: "Ulric", army: 4900 },
117. { kingdom: "Stonegate", general: "Doran", army: 70000 },
118. { kingdom: "YorkenShire", general: "Quinn", army: 0 },
119. { kingdom: "YorkenShire", general: "Quinn", army: 2000 },
120. { kingdom: "Maiden Way", general: "Berinon", army: 100000 }],
121. [["YorkenShire", "Quinn", "Stonegate", "Ulric"],
122. ["Stonegate", "Ulric", "Stonegate", "Doran"],
123. ["Stonegate", "Doran", "Maiden Way", "Merek"],
124. ["Stonegate", "Ulric", "Maiden Way", "Merek"],
125. ["Maiden Way", "Berinon", "Stonegate", "Ulric"]]
126. ])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top