viligen

ladyBugs

Apr 21st, 2022
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ladybug(array_input) {
  2.     len = array_input[0]
  3.     const field = Array(len).fill(0)
  4.  
  5.     let bugs_idxs = array_input[1]
  6.     for (let idx of bugs_idxs.split(" ")) {
  7.         if (idx < 0 || idx >= len) { continue }
  8.         field[idx] = 1
  9.     }
  10.     let array_commands = array_input.slice(2, )
  11.  
  12.     for (let command of array_commands) {
  13.         comand_split = command.split(' ')
  14.         let ladybug_idx = Number(comand_split[0])
  15.         let direction = comand_split[1]
  16.         let move_num = Number(comand_split[2])
  17.         if (ladybug_idx < 0 || ladybug_idx >= len || field[ladybug_idx] == 0) { continue }
  18.         field[ladybug_idx] = 0
  19.         let new_idx = direction == 'right' ? ladybug_idx + move_num : ladybug_idx - move_num
  20.         if (new_idx < 0 || new_idx >= len) { continue }
  21.         if (field[new_idx] == 0) {
  22.             field[new_idx] = 1
  23.         } else {
  24.             while (field[new_idx] == 1) {
  25.  
  26.                 new_idx = direction == 'right' ? new_idx + move_num : new_idx - move_num
  27.                 if (new_idx < 0 || new_idx >= len) { break }
  28.             }
  29.             if (new_idx < 0 || new_idx >= len) { continue }
  30.             field[new_idx] = 1
  31.         }
  32.  
  33.     }
  34.     console.log(field.join(' '))
  35. }
Advertisement
Add Comment
Please, Sign In to add comment