Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ladybug(array_input) {
- len = array_input[0]
- const field = Array(len).fill(0)
- let bugs_idxs = array_input[1]
- for (let idx of bugs_idxs.split(" ")) {
- if (idx < 0 || idx >= len) { continue }
- field[idx] = 1
- }
- let array_commands = array_input.slice(2, )
- for (let command of array_commands) {
- comand_split = command.split(' ')
- let ladybug_idx = Number(comand_split[0])
- let direction = comand_split[1]
- let move_num = Number(comand_split[2])
- if (ladybug_idx < 0 || ladybug_idx >= len || field[ladybug_idx] == 0) { continue }
- field[ladybug_idx] = 0
- let new_idx = direction == 'right' ? ladybug_idx + move_num : ladybug_idx - move_num
- if (new_idx < 0 || new_idx >= len) { continue }
- if (field[new_idx] == 0) {
- field[new_idx] = 1
- } else {
- while (field[new_idx] == 1) {
- new_idx = direction == 'right' ? new_idx + move_num : new_idx - move_num
- if (new_idx < 0 || new_idx >= len) { break }
- }
- if (new_idx < 0 || new_idx >= len) { continue }
- field[new_idx] = 1
- }
- }
- console.log(field.join(' '))
- }
Advertisement
Add Comment
Please, Sign In to add comment