georgiev955

10. Ladybugs

May 27th, 2023
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ladyBug(input) {
  2.     //създаваме масив и го запълваме с 0;
  3.     let field = new Array(input[0]).fill(0);
  4.     let initialPositions = input[1].split(' ');
  5.  
  6.     //нанасяме калинките на позициите им по условие;
  7.     for (let index = 0; index < initialPositions.length; index++) {
  8.         let position = initialPositions[index];
  9.         if (position >= 0 && position < field.length) {
  10.             field[position] = 1;
  11.         }
  12.     }
  13.  
  14.    
  15.     for (let index = 2; index < input.length; index++) {
  16.         let commandArray = input[index].split(' ');
  17.         let currentPosition = Number(commandArray[0]);
  18.         let flyLength = Number(commandArray[2]);
  19.         let direction = commandArray[1];
  20.  
  21.         if (direction === 'left') {
  22.             //първо проверка дали има калинка на съответния индекс;
  23.             if (field[currentPosition] === 1) {
  24.                 field[currentPosition] = 0;
  25.                 currentPosition -= flyLength;
  26.                 //местим я на ляво със съответната стъпка докато не намерим празно място;
  27.                 while (field[currentPosition] === 1) {
  28.                     currentPosition -= flyLength;
  29.                     //ако индексът излезе от масива излизаме от while цикъла, тоест за нея няма място и напуска масива
  30.                     if (currentPosition < 0) {
  31.                         break;
  32.                     }
  33.                 }
  34.                 //ако сме намерили подходящ индекс, сменяме стойността от 0 на 1
  35.                 if (currentPosition >= 0) {
  36.                     field[currentPosition] = 1;
  37.                 }
  38.             }
  39.         } else {
  40.             //ако движим на дясно отново първо проверка дали има калинка на индекса;
  41.             if (field[currentPosition] === 1) {
  42.                 field[currentPosition] = 0;
  43.                 currentPosition += flyLength;
  44.                 //по същия начим местим със стъпката flyLength докато не намерим място или не излезем от масива
  45.                 while (field[currentPosition] === 1) {
  46.                     currentPosition += flyLength;
  47.                     if (currentPosition > (field.length - 1)) {
  48.                         break;
  49.                     }
  50.                 }
  51.                 //ако сме намерили място преди да излезем от масива сменяме стойността от 0 на 1;
  52.                 if (currentPosition < field.length) {
  53.                     field[currentPosition] = 1;
  54.                 }
  55.             }
  56.         }
  57.  
  58.  
  59.     }
  60.     console.log(field.join(' '))
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment