Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve2(input=[]) {
  2.     let arr = input.shift().split('|').map(Number);
  3.     let points = 0;
  4.  
  5.     while (input.length > 0) {
  6.         let commands = input.shift().split('@');
  7.         let direction = commands[0];
  8.         let indexStart = Number(commands[1]);
  9.         let toTraverse = Number(commands[2]);
  10.         let targetPoints = 5;
  11.         let indexToShoot = 0;
  12.  
  13.         if (indexStart < 0 || indexStart >= arr.length) {
  14.             continue;
  15.         }
  16.  
  17.         if (direction === 'Shoot Left') {
  18.             if (indexStart - toTraverse >= 0) {
  19.                 indexToShoot = indexStart - toTraverse;
  20.             } else {
  21.                 let modulo = toTraverse - (arr.length - (arr.length-indexStart));
  22.                 indexToShoot = arr.length - (modulo % arr.length);
  23.             }
  24.         } else if (direction === 'Shoot Right') {
  25.             if (indexStart + toTraverse < arr.length) {
  26.                 indexToShoot = indexStart + toTraverse;
  27.             } else {
  28.                 let modulo = toTraverse - (arr.length-indexStart);
  29.                 indexToShoot = modulo % arr.length;
  30.             }
  31.         }
  32.  
  33.         if (arr[indexToShoot] < 5) {
  34.             targetPoints = arr[indexToShoot];
  35.         }
  36.  
  37.         if (direction === 'Shoot Left') {
  38.             arr.splice(indexToShoot, 1, arr[indexToShoot]-targetPoints);
  39.             points += targetPoints;
  40.         } else if (direction === 'Shoot Right') {
  41.             arr.splice(indexToShoot, 1, arr[indexToShoot]-targetPoints);
  42.             points += targetPoints;
  43.         }
  44.  
  45.         if (direction === 'Reverse') {
  46.             arr.reverse();
  47.         }
  48.  
  49.         if (direction === 'Game over') {
  50.             console.log(arr.join(' - '));
  51.             console.log(`Iskren finished the archery tournament with ${points} points!`);  
  52.             break;
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement