Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ladybugs(input) {
- let fieldSize = Number(input[0]);
- let ladybugsPositions = input[1].split(' ');
- let initialField = [];
- //initial field array
- for(let i = 0; i < fieldSize; i++) {
- initialField[i] = 0;
- }
- for(let i = 0; i < ladybugsPositions.length; i++) {
- let ladybugIndex = Number(ladybugsPositions[i]);
- for(let j = 0; j < fieldSize; j++) {
- if(j == ladybugIndex) {
- initialField[j] = 1;
- }
- }
- }
- //commands
- for(let i = 2; i < input.length; i++) {
- let command = input[i].split(' ');
- let ladybug = Number(command[0]);
- let direction = command[1];
- let path = Number(command[2]);
- if(initialField[ladybug] == 1) {
- initialField[ladybug] = 0;
- let destination = 0;
- if(direction == 'right') {
- destination = ladybug + path;
- while(initialField[destination] == 1) {
- destination += path;
- }
- } else if(direction == 'left') {
- destination = ladybug - path;
- while(initialField[destination] == 1) {
- destination -= path;
- }
- }
- if(initialField[destination] == 0) {
- initialField[destination] = 1;
- }
- }
- }
- console.log(initialField.join(' '));
- }
- ladybugs([ 5, '3',
- '3 left 2',
- '1 left -2']
- );
Advertisement
Add Comment
Please, Sign In to add comment