Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let size = Number(input.shift());
- let field = [];
- field.length = size;
- field.fill(0);
- const isValidIndex = (index, arr) => {
- return index >= 0 && index <= arr.length - 1;
- };
- const success = (position, arr) => {
- if (position < 0 || position >= size) {
- return true;
- } else if (arr[position] === 0) {
- arr[position] = 1;
- return true;
- }
- return false;
- };
- let initials = input
- .shift()
- .split(" ")
- .map(Number);
- for (const index of initials) {
- if (isValidIndex(index, field)) {
- field[index] = 1;
- }
- }
- for (const line of input) {
- let data = line.split(" ");
- let start = Number(data[0]);
- let direction = data[1];
- let change =
- direction.toLowerCase() === "right"
- ? Number(data[2])
- : Number(data[2]) * -1;
- let position = start + change;
- if (field[start] === 1) {
- field[start] = 0;
- while (!success(position, field)) {
- position += change;
- }
- }
- }
- console.log(field.join(" "));
- }
Advertisement
Add Comment
Please, Sign In to add comment