Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input){
- class Gladiator{
- constructor(name){
- this.name = name;
- this.technique = [];
- this.skill = [];
- }
- updateSet(techVal, skillVal){
- let index = this.technique.indexOf(techVal);
- if(index === -1){
- this.technique.push(techVal);
- this.skill.push(Number(skillVal));
- }else{
- if(this.skill[index] < Number(skillVal)){
- this.skill[index] = Number(skillVal);
- }
- }
- }
- getScore(){
- return this.skill.reduce((a,b)=>{
- return a + b;
- });
- }
- sortBoth(){
- let helpMap = new Map();
- for (const one of this.technique) {
- let index = this.technique.indexOf(one);
- helpMap.set(one,this.skill[index]);
- }
- let helpertech = [];
- let helperSkill = [];
- Array.from(helpMap).sort((a,b)=>{
- if(a[1] === b[1]){
- return a[0].localeCompare(b[0])
- }else{
- return b[1] - a[1];
- }
- }).forEach(pair=>{
- helpertech.push(pair[0]);
- helperSkill.push(pair[1]);
- });
- this.technique = [];
- this.technique.push(...helpertech);
- this.skill = [];
- this.skill.push(...helperSkill);
- }
- }
- let pool = [];
- let nameSet = [];
- for (const line of input) {
- if(line === 'Ave Cesar'){
- break;
- }
- if(line.match(' -> ')){
- let [name, technique, skill] = line.split(' -> ');
- let index = nameSet.indexOf(name);
- if(index === -1){
- let obj = new Gladiator(name);
- obj.updateSet(technique,skill);
- pool.push(obj);
- nameSet.push(name);
- }else{
- let current = pool[index];
- current.updateSet(technique,skill);
- pool[index] = current;
- }
- }else{
- let [nameOne, nameTwo] = line.split(' vs ');
- let indexOne = nameSet.indexOf(nameOne);
- let indexTwo = nameSet.indexOf(nameTwo);
- if(indexOne > -1 && indexTwo > -1){
- let p1 = pool[indexOne];
- let p2 = pool[indexTwo];
- let isFight = false;
- for (const one of p1.technique) {
- if(p2.technique.includes(one)){
- isFight = true;
- break;
- }
- }
- if(isFight === true){
- let p1Score = p1.getScore();
- let p2Score = p2.getScore();
- if(p1Score > p2Score){
- pool.splice(indexTwo,1);
- nameSet.splice(indexTwo,1);
- }else if(p2Score > p1Score){
- pool.splice(indexOne,1);
- nameSet.splice(indexOne,1);
- }
- }
- }
- }
- }
- pool.sort((a,b)=>b.getScore()-a.getScore());
- pool.forEach(object=>{
- object.sortBoth();
- console.log(`${object.name}: ${object.getScore()} skill`);
- object.technique.forEach((item,index)=>{
- console.log(`- ${item} <!> ${object.skill[index]}`)
- })
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment