AreWe

Archery Tournament

Jun 24th, 2020
1,509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. archeryTournament = (input) => {
  2.     let points = 0;
  3.     let targets = input.shift().split('|').map(Number);
  4.     while((line = input.shift()) !== 'Game over') {
  5.         if(line === 'Reverse') {
  6.             targets.reverse();
  7.         } else {
  8.             let [ direction, index, length ] = line.split(' ')[1].split('@');
  9.             index = Number(index);
  10.             length = Number(length);
  11.             if(targets[index] === undefined) {
  12.                 continue;
  13.             }
  14.  
  15.             while(length-- > 0) {
  16.                 index = index + (direction === 'Left' ? -1 : 1);
  17.                 if(index === -1) {
  18.                     index = targets.length - 1;
  19.                 } else if (index === targets.length) {
  20.                     index = 0;
  21.                 }
  22.             }
  23.  
  24.             points += targets[index] >= 5 ? 5 : targets[index];
  25.             targets[index] = (value = targets[index] - 5) >= 0 ? value : 0;
  26.         }
  27.     }
  28.  
  29.     console.log(targets.join(' - '));
  30.     console.log(`Iskren finished the archery tournament with ${points} points!`);
  31. };
Advertisement
Add Comment
Please, Sign In to add comment