Todorov_Stanimir

LadyBugs

Aug 12th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ladyBugs(input) {
  2.     let field = [];
  3.     let fieldsize = Number(input.shift());
  4.     let indexesWithLadyBugs = input.shift().split(' ').map(Number);
  5.     for (let i = 0; i < fieldsize; i++) {
  6.         (indexesWithLadyBugs.includes(i)) ? field[i] = 1 : field[i] = 0
  7.     };
  8.     let line = input.shift();
  9.     while (line !== 'end') {
  10.         let [startIndex, direction, step] = line.split(' ');
  11.         startIndex = Number(startIndex);
  12.         step = Number(step);
  13.         let finishedIndex = startIndex;
  14.         if (field[startIndex] === 1) {
  15.             field[startIndex] = 0
  16.             if (direction === 'right') {
  17.                 finishedIndex= startIndex+step;
  18.                 while (field[finishedIndex] === 1) {
  19.                     finishedIndex += step;
  20.                 }
  21.             } else {
  22.                 finishedIndex= startIndex-step;
  23.                 while (field[finishedIndex] === 1) {
  24.                     finishedIndex -= step;
  25.                 }
  26.             }
  27.             if (finishedIndex <= field.length - 1 && finishedIndex >= 0) {
  28.                 field[finishedIndex] = 1;
  29.             }
  30.         }
  31.         line = input.shift();
  32.     }
  33.     console.log(field.join(' '));
  34. }
Advertisement
Add Comment
Please, Sign In to add comment