Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     input.pop();
  3.     let warriorsAndStrength = {};
  4.     let warriors = [];
  5.  
  6.     for (const line of input) {
  7.         let command = line.split(':');
  8.        
  9.         if (command[0] === 'Add') {
  10.             if (!warriorsAndStrength.hasOwnProperty(command[1])) {
  11.                 warriorsAndStrength[command[1]] = {
  12.                     health: Number(command[2]),
  13.                     strength: Number(command[3])
  14.                 }
  15.             } else {
  16.                     warriorsAndStrength[command[1]] = {
  17.                         health: Number(command[2]) + Number(command[2]),
  18.                         strength: Number(command[3])
  19.                     }
  20.             }
  21.         }
  22.  
  23.         if (command[0] === 'Attack') {
  24.             if (Object.keys(warriorsAndStrength).indexOf(command[1]) !== -1 && Object.keys(warriorsAndStrength).indexOf(command[2]) !== -1) {
  25.                 let decreased = Object.values(warriorsAndStrength[command[2]])
  26.                 let increased = Object.values(warriorsAndStrength[command[1]])
  27.                 let healthIncrease = increased[0]
  28.                 let strengthIncrease = increased[1]
  29.                 let health = decreased[0]
  30.                 let strength = decreased[1]
  31.                 warriorsAndStrength[command[2]] = {
  32.                     health: health - Number(command[3]),
  33.                     strength: strength
  34.                 }
  35.                 warriorsAndStrength[command[1]] = {
  36.                     health: healthIncrease,
  37.                     strength: strengthIncrease - 1
  38.                 }
  39.                 let resultDef = warriorsAndStrength[command[2]].health
  40.                 let resultAtk = warriorsAndStrength[command[1]].strength
  41.                 if (resultDef <= 0) {
  42.                     delete warriorsAndStrength[command[2]]
  43.                     console.log(`${command[2]} was disqualified!`)
  44.                 }
  45.  
  46.                 if (resultAtk <= 0) {
  47.                     delete warriorsAndStrength[command[1]];
  48.                     console.log(`${command[1]} was disqualified!`)
  49.                 }
  50.             }
  51.         }
  52.  
  53.         if (command[0] === 'Delete') {
  54.             if (Object.keys(warriorsAndStrength).indexOf(command[1]) !== -1) {
  55.                 delete warriorAndStrength[command[1]]
  56.             }
  57.  
  58.             if (command[1] === 'All') {
  59.                 warriorsAndStrength = {};
  60.             }
  61.         }
  62.     }
  63.  
  64.     let warriorsArr = Array.from(Object.entries(warriorsAndStrength))
  65.     warriorsArr.reverse();
  66.     let count = warriorsArr.length
  67.     console.log(`People count: ${count}`)
  68.  
  69.     for (let i = 0; i < warriorsArr.length; i++) {
  70.         let warrior = warriorsArr[i];
  71.         let values = Array.from(Object.values(warrior[1]))
  72.         let combine = [].concat.apply([], values)
  73.         combine = combine.map((value) => '- ' + value)
  74.         console.log(`${warrior[0]} ${combine.join(' ')}`)
  75.        
  76.     }
  77. }
  78. solve([
  79.   'Add:Bonnie:3000:5',
  80.   'Add:Johny:4000:10',
  81.   'Attack:Johny:Bonnie:400',
  82.   'Add:Chicken:1000:1',
  83.   'Add:Rabbit:3000:5',
  84.   'Add:Buggy:1259:10',
  85.   'Attack:Chicken:Rabbit:1000',
  86.   'Results'
  87. ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement