View difference between Paste ID: 81HvLMnx and 6kM5sAav
SHOW: | | - or go back to the newest paste.
1
function solve (input) {
2
	let fieldSize = +(input[0]);
3
	let bugs = input[1].split(' ').map(el => +(el));
4
	if (input[1] === '') {
5
		bugs = [];
6
	}
7
	let commands = input.slice(2);
8
	let field = [];
9
	
10
	// initial field
11
	for (let i = 0; i < fieldSize; i++) {
12
		bugs.indexOf(i) !== -1 ? field.push(1) : field.push(0);
13
	}
14
	
15
	for (let c in commands) {
16
		let temp = commands[c].split(' ');
17
		let position = +(temp[0]);
18
		if (commands[c] === '' || position < 0 || position >= fieldSize || field[position] !== 1) {
19
			continue;
20
		}
21
		let right = 'right' === temp[1];
22
		let step = +(temp[2]);
23
		field[position] = 0; // Bug flies away
24
		
25
		while (position >= 0 && position < fieldSize) { // while flying in the field
26
			right ? position += step : position -= step;  // go right or left
27
			
28
			if (field[position] === 0) { // check for empty spot
29
				field[position] = 1;
30
				break;
31
			}
32
		}
33
	}
34
	console.log(field.join(' '));
35
}