Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let fieldSize = input.shift();
- let indexes = input.shift().split(' ').map(el => Number(el));
- let field = [];
- field = field.slice(0, fieldSize);
- if (field.length < fieldSize) {
- let addField = fieldSize - field.length;
- for (let i = 0; i < addField; i++) {
- if (!indexes.includes(i)) {
- field.push(0);
- } else {
- field.push(1);
- }
- }
- }
- // console.log(field);
- for (let el of input) {
- let cur = el.split(' ');
- let startIndex = Number(cur[0]);
- let direction = cur[1];
- let flyLength = Number(cur[2]);
- let landingIndex = startIndex + flyLength;
- if (direction === 'left') {
- landingIndex = startIndex - flyLength;
- }
- if (direction === 'right') {
- while (field[landingIndex] !== 0 && field[landingIndex] !== undefined) {
- landingIndex++;
- }
- if (field[landingIndex] !== undefined) {
- field[landingIndex] = 1;
- field[startIndex] = 0;
- startIndex = landingIndex;
- } else {
- field[startIndex] = 0;
- }
- } else if (direction === 'left') {
- while (field[landingIndex] !== 0 && field[landingIndex] !== undefined) {
- landingIndex--;
- }
- if (field[landingIndex] !== undefined) {
- field[landingIndex] = 1;
- field[startIndex] = 0;
- startIndex = landingIndex;
- } else {
- field[startIndex] = 0;
- }
- }
- }
- console.log(field.join(' '));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement