Advertisement
kstoyanov

02. Expedition js exam

Aug 3rd, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(primary, secondary, targets, startingPoint) {
  2.   let steps = 1;
  3.   const prMRows = primary.length;
  4.   const prMCols = primary[0].length;
  5.   const secMRows = secondary.length;
  6.   const secMCols = secondary[0].length;
  7.  
  8.   const modifyPrimary = (coordinates) => {
  9.     const row = Number(coordinates[0]);
  10.     const col = Number(coordinates[1]);
  11.     for (let i = 0; i < secMRows; i++) {
  12.       if (i + row < prMRows) {
  13.         for (let j = 0; j < secMCols; j++) {
  14.           if (primary[i + row][j + col] !== undefined && secondary[i][j] === 1) {
  15.             primary[i + row][j + col] = primary[i + row][j + col] === 0 ? 1 : 0;
  16.           }
  17.         }
  18.       }
  19.     }
  20.   };
  21.  
  22.   const definePos = (currentPosition) => {
  23.     const currentRow = currentPosition[0];
  24.     const currentCol = currentPosition[1];
  25.  
  26.     if (currentRow === 0) {
  27.       console.log('Top');
  28.     } else if (currentRow === prMRows - 1) {
  29.       console.log('Bottom');
  30.     } else if (currentCol === 0) {
  31.       console.log('Left');
  32.     } else if (currentCol === prMCols - 1) {
  33.       console.log('Right');
  34.     } else if (currentRow < prMRows / 2 && currentCol >= prMCols / 2) {
  35.       console.log('Dead end 1');
  36.     } else if (currentRow < prMRows / 2 && currentCol < prMCols / 2) {
  37.       console.log('Dead end 2');
  38.     } else if (currentRow >= prMRows / 2 && currentCol < prMCols / 2) {
  39.       console.log('Dead end 3');
  40.     } else if (currentRow >= prMRows / 2 && currentCol >= prMCols / 2) {
  41.       console.log('Dead end 4');
  42.     }
  43.   };
  44.  
  45.   targets.forEach((target) => {
  46.     modifyPrimary(target);
  47.   });
  48.  
  49.   let currentPosition = [startingPoint[0], startingPoint[1]];
  50.   let previousDirection;
  51.   const move = true;
  52.  
  53.   while (move) {
  54.     if (currentPosition[0] + 1 < prMRows && primary[currentPosition[0] + 1][currentPosition[1]] === 0 && previousDirection !== 'up') {
  55.       currentPosition = [currentPosition[0] + 1, currentPosition[1]];
  56.       previousDirection = 'down';
  57.     } else if (currentPosition[1] + 1 < prMCols && primary[currentPosition[0]][currentPosition[1] + 1] === 0 && previousDirection !== 'left') {
  58.       currentPosition = [currentPosition[0], currentPosition[1] + 1];
  59.       previousDirection = 'right';
  60.     } else if (currentPosition[0] > 0 && primary[currentPosition[0] - 1][currentPosition[1]] === 0 && previousDirection !== 'down') {
  61.       currentPosition = [currentPosition[0] - 1, currentPosition[1]];
  62.       previousDirection = 'up';
  63.     } else if (currentPosition[1] > 0 && primary[currentPosition[0]][currentPosition[1] - 1] === 0 && previousDirection !== 'right') {
  64.       currentPosition = [currentPosition[0], currentPosition[1] - 1];
  65.       previousDirection = 'left';
  66.     } else {
  67.       break;
  68.     }
  69.     steps++;
  70.   }
  71.  
  72.   console.log(steps);
  73.   definePos(currentPosition);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement