Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(primary, secondary, targets, startingPoint) {
- let steps = 1;
- const prMRows = primary.length;
- const prMCols = primary[0].length;
- const secMRows = secondary.length;
- const secMCols = secondary[0].length;
- const modifyPrimary = (coordinates) => {
- const row = Number(coordinates[0]);
- const col = Number(coordinates[1]);
- for (let i = 0; i < secMRows; i++) {
- if (i + row < prMRows) {
- for (let j = 0; j < secMCols; j++) {
- if (primary[i + row][j + col] !== undefined && secondary[i][j] === 1) {
- primary[i + row][j + col] = primary[i + row][j + col] === 0 ? 1 : 0;
- }
- }
- }
- }
- };
- const definePos = (currentPosition) => {
- const currentRow = currentPosition[0];
- const currentCol = currentPosition[1];
- if (currentRow === 0) {
- console.log('Top');
- } else if (currentRow === prMRows - 1) {
- console.log('Bottom');
- } else if (currentCol === 0) {
- console.log('Left');
- } else if (currentCol === prMCols - 1) {
- console.log('Right');
- } else if (currentRow < prMRows / 2 && currentCol >= prMCols / 2) {
- console.log('Dead end 1');
- } else if (currentRow < prMRows / 2 && currentCol < prMCols / 2) {
- console.log('Dead end 2');
- } else if (currentRow >= prMRows / 2 && currentCol < prMCols / 2) {
- console.log('Dead end 3');
- } else if (currentRow >= prMRows / 2 && currentCol >= prMCols / 2) {
- console.log('Dead end 4');
- }
- };
- targets.forEach((target) => {
- modifyPrimary(target);
- });
- let currentPosition = [startingPoint[0], startingPoint[1]];
- let previousDirection;
- const move = true;
- while (move) {
- if (currentPosition[0] + 1 < prMRows && primary[currentPosition[0] + 1][currentPosition[1]] === 0 && previousDirection !== 'up') {
- currentPosition = [currentPosition[0] + 1, currentPosition[1]];
- previousDirection = 'down';
- } else if (currentPosition[1] + 1 < prMCols && primary[currentPosition[0]][currentPosition[1] + 1] === 0 && previousDirection !== 'left') {
- currentPosition = [currentPosition[0], currentPosition[1] + 1];
- previousDirection = 'right';
- } else if (currentPosition[0] > 0 && primary[currentPosition[0] - 1][currentPosition[1]] === 0 && previousDirection !== 'down') {
- currentPosition = [currentPosition[0] - 1, currentPosition[1]];
- previousDirection = 'up';
- } else if (currentPosition[1] > 0 && primary[currentPosition[0]][currentPosition[1] - 1] === 0 && previousDirection !== 'right') {
- currentPosition = [currentPosition[0], currentPosition[1] - 1];
- previousDirection = 'left';
- } else {
- break;
- }
- steps++;
- }
- console.log(steps);
- definePos(currentPosition);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement