Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr) {
- let fieldSize = arr.shift();
- let field = new Array(fieldSize).fill(0, 0, fieldSize);
- let ladyBugIndexes = arr[0].split(' ').map(Number);
- for (let i = 0; i < ladyBugIndexes.length; i++) {
- if (ladyBugIndexes[i] < 0 || ladyBugIndexes[i] >= fieldSize) {
- ladyBugIndexes.splice(i, 1);
- i--;
- }
- }
- for (let i = 0; i < ladyBugIndexes.length; i++) {
- let ladybugIndex = ladyBugIndexes[i];
- field[ladybugIndex] = 1;
- }
- for (let i = 1; i < arr.length; i++) {
- let tokens = arr[i].split(' ');
- let index = Number(tokens[0]);
- let direction = tokens[1];
- let flightLength = Number(tokens[2]);
- if (field[index] === 1) {
- field[index] = 0;
- while (index >= 0 && index < field.length) {
- if (direction === 'right') {
- index += flightLength;
- } else if (direction === 'left') {
- index -= flightLength;
- }
- if (field[index] === 0) {
- field[index] = 1;
- break;
- }
- }
- }
- }
- console.log(field.join(' '));
- }
Advertisement
Add Comment
Please, Sign In to add comment