maymunskoa

Orbit

Feb 7th, 2020
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(inputArr){
  2.     let row = inputArr[0];
  3.     let col = inputArr[1];
  4.     let startRow = inputArr[2];
  5.     let startCol = inputArr[3];
  6.  
  7.     const sqaredSize = row*col;
  8.     let currSize = 1;
  9.     let currentNumber = 1;
  10.  
  11.     //ARRAY FIXING
  12.     let arr = new Array(row);
  13.     for(let i = 0; i < row; i++){
  14.         arr[i] = new Array(col);
  15.     }
  16.     arr[startRow].fill(currentNumber, startCol, startCol+1);
  17.     //ARRAY FIXING End
  18.  
  19.  
  20.  
  21.     //initial fixing of rows and cols
  22.     let topRow = startRow - 1;
  23.     if(topRow < 0) {
  24.         topRow = 0;
  25.     }
  26.  
  27. let bottomRow = startRow + 1;
  28. if(bottomRow >= row){
  29.     bottomRow = row-1;
  30. }
  31.  
  32. let leftCol = startCol - 1;
  33. if(leftCol < 0){
  34.     leftCol = 0;
  35. }
  36.  
  37. let rightCol = startCol + 1;
  38. if(rightCol >= col){
  39.     rightCol = col-1;
  40. }
  41.     //
  42.  
  43.  
  44.  
  45.     while(currSize < sqaredSize){
  46.         currentNumber +=1;
  47.         let lengthRow = bottomRow - topRow + 1;
  48.         let lenghtCol = rightCol - leftCol + 1;
  49.         currSize = lengthRow*lenghtCol;
  50.  
  51.  
  52.        
  53.         //top and bottom row (left col to right col)
  54.         for(let i = leftCol; i <= rightCol; i+=1){
  55.             if(arr[topRow][i] === undefined){
  56.                 arr[topRow].fill(currentNumber,i,i+1);
  57.             }
  58.             if(arr[bottomRow][i] === undefined){
  59.                 arr[bottomRow].fill(currentNumber,i,i+1);
  60.             }
  61.            
  62.         }
  63.         //left and right col (top row to bottom row)
  64.         for(let j = topRow+1; j < bottomRow; j+=1){
  65.             if(arr[j][rightCol] === undefined){
  66.                 arr[j][rightCol] = currentNumber;
  67.             }
  68.             if(arr[j][leftCol] === undefined){
  69.                 arr[j][leftCol] = currentNumber;
  70.             }
  71.            
  72.         }
  73.  
  74.         leftCol -=1;
  75.         if(leftCol < 0){
  76.             leftCol = 0;
  77.         }
  78.        
  79.  
  80.         rightCol +=1;
  81.         if(rightCol >= col){
  82.             rightCol = col-1;
  83.         }
  84.  
  85.         topRow -=1;
  86.         if(topRow < 0) {
  87.             topRow = 0;
  88.         }
  89.  
  90.         bottomRow +=1;
  91.         if(bottomRow >= row){
  92.             bottomRow = row-1;
  93.         }
  94.     }
  95.  
  96.     arr.map((item)=>{
  97.         console.log(item.join(' '));
  98.     })
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment