didkoslawow

Untitled

Dec 23rd, 2022
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. function ladybugsFlying(inputInfo) {
  2. // building the logic for the starting field
  3.  
  4. let field = new Array(inputInfo[0]);
  5. field.fill(0);
  6. let startingIndexes = inputInfo[1].split(" ");
  7.  
  8. for (let i = 0; i < startingIndexes.length; i++) {
  9. for (let j = 0; j < startingIndexes.length; j++) {
  10. field[startingIndexes[j]] = 1;
  11. }
  12. }
  13.  
  14. // building the logic for the ladybugs to fly around
  15.  
  16. for (let i = 2; i < inputInfo.length; i++) {
  17. let command = inputInfo[i].split(" ");
  18. if (field[command[0]] === 1) {
  19. if (command[1] === "left") {
  20. for (let j = field.length - 1; j <= command[2] ; j++) {
  21. field[command[0]] = 0;
  22. if (field[j] === 0) {
  23. field[Number(command[0]) - Number(command[2])] = 1;
  24. }
  25. }
  26. } else if (command[1] === "right") {
  27. field[command[0]] = 0;
  28. for (let k = 0; k < field.length; k++) {
  29. if (field[k] === 0 && field.length < inputInfo[0]) {
  30. field[Number(command[0]) + Number(command[2])] = 1;
  31. }
  32. }
  33. }
  34. }
  35. }
  36. console.log(field.join(" "));
  37. }
Advertisement
Add Comment
Please, Sign In to add comment