Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ladybugsFlying(inputInfo) {
- // building the logic for the starting field
- let field = new Array(inputInfo[0]);
- field.fill(0);
- let startingIndexes = inputInfo[1].split(" ");
- for (let i = 0; i < startingIndexes.length; i++) {
- for (let j = 0; j < startingIndexes.length; j++) {
- field[startingIndexes[j]] = 1;
- }
- }
- // building the logic for the ladybugs to fly around
- for (let i = 2; i < inputInfo.length; i++) {
- let command = inputInfo[i].split(" ");
- if (field[command[0]] === 1) {
- if (command[1] === "left") {
- for (let j = field.length - 1; j <= command[2] ; j++) {
- field[command[0]] = 0;
- if (field[j] === 0) {
- field[Number(command[0]) - Number(command[2])] = 1;
- }
- }
- } else if (command[1] === "right") {
- field[command[0]] = 0;
- for (let k = 0; k < field.length; k++) {
- if (field[k] === 0 && field.length < inputInfo[0]) {
- field[Number(command[0]) + Number(command[2])] = 1;
- }
- }
- }
- }
- }
- console.log(field.join(" "));
- }
Advertisement
Add Comment
Please, Sign In to add comment