Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve (input) {
- let fieldSize = +(input[0]);
- let bugs = input[1].split(' ').map(el => +(el));
- if (input[1] === '') {
- bugs = [];
- }
- let commands = input.slice(2);
- let field = [];
- // initial field
- for (let i = 0; i < fieldSize; i++) {
- bugs.indexOf(i) !== -1 ? field.push(1) : field.push(0);
- }
- for (let c in commands) {
- let temp = commands[c].split(' ');
- let position = +(temp[0]);
- if (commands[c] === '' || position < 0 || position >= fieldSize || field[position] !== 1) {
- continue;
- }
- let right = 'right' === temp[1];
- let step = +(temp[2]);
- field[position] = 0; // Bug flies away
- while (position >= 0 && position < fieldSize) { // while flying in the field
- right ? position += step : position -= step; // go right or left
- if (field[position] === 0) { // check for empty spot
- field[position] = 1;
- break;
- }
- }
- }
- console.log(field.join(' '));
- }
Add Comment
Please, Sign In to add comment