Advertisement
dpeeva

Ladybugs failing

Jun 23rd, 2022
664
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     const field = []
  3.     const size = input[0]
  4.     for (let i = 0; i < size; i++) {
  5.         field[i] = 0
  6.     }
  7.     const ladybugs = input[1].split(" ").map(Number)
  8.     ladybugs.map(index => field[index] = 1)
  9.  
  10.     let commands = input.slice(2)
  11.     const length = commands.length
  12.  
  13.  
  14.     let command = ""
  15.     let bugIndex = 0
  16.     let targetIndex = 0
  17.     let dir = ""
  18.     let fly = 0
  19.  
  20.     for (let i = 0; i < commands.length; i++) {
  21.         command = commands[i].split(" ")
  22.         bugIndex = Number(command[0])
  23.         dir = command[1]
  24.         targetIndex = Number(command[2])
  25.  
  26.         if (dir === "right") {
  27.             fly = bugIndex + targetIndex
  28.         }
  29.         if (dir === "left") {
  30.             fly = bugIndex - targetIndex
  31.         }
  32.  
  33.         if (fly > size - 1) {
  34.             field.splice(bugIndex, 1, 0)
  35.             continue
  36.         }
  37.  
  38.         while (fly <= size - 1) {
  39.             field.splice(bugIndex, 1, 0)
  40.             // BUG INSIDE
  41.             if (field[fly] === 1) {
  42.                 fly += targetIndex
  43.                 continue
  44.             }
  45.             // PLACING BUG
  46.             else {
  47.                 field[fly] = 1
  48.                 break
  49.             }
  50.         }
  51.     }
  52.  
  53.     console.log(field.join(" "))
  54. }
Advertisement
RAW Paste Data Copied
Advertisement