Advertisement
Guest User

Untitled

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