nikolayneykov

Untitled

Feb 28th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     let fieldSize = arr.shift();
  3.     let field = new Array(fieldSize).fill(0, 0, fieldSize);
  4.     let ladyBugIndexes = arr[0].split(' ').map(Number);
  5.  
  6.     for (let i = 0; i < ladyBugIndexes.length; i++) {
  7.         if (ladyBugIndexes[i] < 0 || ladyBugIndexes[i] >= fieldSize) {
  8.             ladyBugIndexes.splice(i, 1);
  9.             i--;
  10.         }
  11.     }
  12.  
  13.     for (let i = 0; i < ladyBugIndexes.length; i++) {
  14.         let ladybugIndex = ladyBugIndexes[i];
  15.         field[ladybugIndex] = 1;
  16.     }
  17.  
  18.     for (let i = 1; i < arr.length; i++) {
  19.         let tokens = arr[i].split(' ');
  20.         let index = Number(tokens[0]);
  21.         let direction = tokens[1];
  22.         let flightLength = Number(tokens[2]);
  23.  
  24.         if (field[index] === 1) {
  25.             field[index] = 0;
  26.  
  27.             while (index >= 0 && index < field.length) {
  28.                 if (direction === 'right') {
  29.                     index += flightLength;
  30.                 } else if (direction === 'left') {
  31.                     index -= flightLength;
  32.                 }
  33.                 if (field[index] === 0) {
  34.                     field[index] = 1;
  35.                     break;
  36.                 }
  37.             }
  38.         }
  39.     }
  40.     console.log(field.join(' '));
  41. }
Advertisement
Add Comment
Please, Sign In to add comment