Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- const field = []
- const size = input[0]
- for (let i = 0; i < size; i++) {
- field[i] = 0
- }
- const ladybugs = input[1].split(" ").map(Number)
- ladybugs.map(index => field[index] = 1)
- let commands = input.slice(2)
- const length = commands.length
- let command = ""
- let bugIndex = 0
- let targetIndex = 0
- let dir = ""
- let fly = 0
- for (let i = 0; i < commands.length; i++) {
- command = commands[i].split(" ")
- bugIndex = Number(command[0])
- dir = command[1]
- targetIndex = Number(command[2])
- if (dir === "right") {
- fly = bugIndex + targetIndex
- }
- if (dir === "left") {
- fly = bugIndex - targetIndex
- }
- if (fly > size - 1) {
- field.splice(bugIndex, 1, 0)
- continue
- }
- while (fly <= size - 1) {
- field.splice(bugIndex, 1, 0)
- // BUG INSIDE
- if (field[fly] === 1) {
- fly += targetIndex
- continue
- }
- // PLACING BUG
- else {
- field[fly] = 1
- break
- }
- }
- }
- console.log(field.join(" "))
- }
Advertisement
RAW Paste Data
Copied
Advertisement