Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let arr = additionalSorting();
- // console.log(arr[1]);
- for (let el of arr) {
- console.log(`${el.shift()}: ${el[0].shift()[1]} skill`);
- // console.log(el);
- for (let el2 of el) {
- // console.log(el2);
- for (let el3 of el2) {
- console.log(`- ${el3[0]} <!> ${el3[1]}`);
- }
- }
- }
- function objectCreation(input) {
- let gladObj = {};
- while (true) {
- if (input[0].split(" -> ").length === 1) {
- break;
- }
- let cur = input.shift().split(" -> ");
- let name = cur[0];
- let technique = cur[1];
- let skill = Number(cur[2]);
- if (gladObj.hasOwnProperty(name)) {
- if (gladObj[name].hasOwnProperty(technique)) {
- if (gladObj[name][technique] < skill) {
- gladObj[name][technique] = skill;
- }
- } else {
- gladObj[name][technique] = skill;
- }
- } else {
- gladObj[name] = { [technique]: skill };
- }
- }
- return gladObj;
- }
- function totalCalc() {
- let gladObj = objectCreation(input);
- for (let key in gladObj) {
- let total = 0;
- for (let key2 in gladObj[key]) {
- total += gladObj[key][key2];
- }
- gladObj[key]["Total"] = total;
- }
- return gladObj;
- }
- function fights(input) {
- let gladObj = totalCalc();
- for (let el of input) {
- let cur = el.split(" vs ");
- let gladOne = cur[0];
- let gladTwo = cur[1];
- if (gladObj.hasOwnProperty(gladOne) && gladObj.hasOwnProperty(gladTwo)) {
- let gladOneTech = Object.keys(gladObj[gladOne]);
- let gladTwoTech = Object.keys(gladObj[gladTwo]);
- let bool = compare(gladOneTech, gladTwoTech);
- if (bool) {
- if (gladObj[gladOne]["Total"] > gladObj[gladTwo]["Total"]) {
- delete gladObj[gladTwo];
- } else {
- delete gladObj[gladOne];
- }
- }
- }
- }
- return gladObj;
- }
- function compare(gladOneTech, gladTwoTech) {
- let bool = gladOneTech.some((el) =>
- el !== "Total" ? gladTwoTech.includes(el) : false
- );
- return bool;
- }
- function arrCreation() {
- let gladObj = fights(input);
- let arr = [];
- let arr2 = [];
- for (let key in gladObj) {
- arr2 = [];
- for (let key2 in gladObj[key]) {
- arr2.push([key2, gladObj[key][key2]]);
- }
- arr.push([key, arr2]);
- }
- return arr;
- }
- function sorting() {
- let arr = arrCreation();
- arr.sort((a, b) =>
- b[1][b[1].length - 1][1] === a[1][a[1].length - 1][1]
- ? a[0].localeCompare(b[0])
- : b[1][b[1].length - 1][1] - a[1][a[1].length - 1][1]
- );
- for (let el of arr) {
- el[1].sort((a, b) =>
- a[1] === b[1] ? a[0].localeCompare(b[0]) : b[1] - a[1]
- );
- }
- return arr;
- }
- function additionalSorting() {
- let arr = sorting();
- for (let el of arr) {
- for (let el2 of el[1]) {
- if (el2[0] === "Total" && el[1].indexOf(el2) !== 0) {
- el[1].splice(el[1].indexOf(el2), 1);
- el[1].unshift(el2);
- }
- }
- }
- return arr;
- }
- }
- solve([
- "Peter -> Duck -> 400",
- "Julius -> Shield -> 150",
- "Gladius -> Heal -> 200",
- "Gladius -> Support -> 250",
- "Gladius -> Shield -> 250",
- "Peter vs Gladius",
- "Gladius vs Julius",
- "Gladius vs Maximilian",
- "Ave Cesar",
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement