Advertisement
stefan_fotev

Lost

Jan 19th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.  
  3.     const arr = args[0].split(' ').map(Number);
  4.  
  5.     const rows = arr[0];
  6.     const cols = arr[1];
  7.  
  8.     const field = new Array(rows);
  9.     field.fill(0);
  10.     for(let i = 0; i < field.length; i += 1) {
  11.         field[i] = args[i + 1].split(' ').map(Number);
  12.     }
  13.  
  14.     let startRow = rows/2 | 0;
  15.     let startCol = cols/2 | 0;
  16.     let canMove = true;
  17.  
  18.     let currentPosition = field[startRow][startCol];
  19.     const mask = 1;
  20.     const directions = ['u', 'r', 'd', 'l'];
  21.  
  22.     while(canMove){
  23.         let binaryPosition = currentPosition.toString(2);
  24.         for(let i = 0; i < binaryPosition.length; i += 1){
  25.             if(((currentPosition & (mask << i)) >> i === 1)){
  26.                 let deltaRow = 0, deltaCol = 0;
  27.                 let dir = directions[i];
  28.                 if(dir === 'u') {
  29.                     deltaRow = -1;
  30.                 }
  31.                 else if(dir === 'd') {
  32.                     deltaRow = +1;
  33.                 }
  34.                 else if(dir === 'l') {
  35.                     deltaCol = -1;
  36.                 }
  37.                 else if(dir === 'r') {
  38.                     deltaCol = +1;
  39.                 }
  40.                 //console.log('moving' + dir);
  41.                 //console.log('length:' + binaryPosition.length);
  42.  
  43.                 let newPosRow = startRow + deltaRow;
  44.                 let newPosCol = startCol + deltaCol;
  45.  
  46.                 if(newPosRow < 0 || newPosRow > rows - 1) {
  47.                     console.log('No rakiya, only JavaScript' + ' ' + startRow + ' ' + startCol);
  48.                     return;
  49.                 }
  50.                 else if(newPosCol < 0 || newPosCol > cols - 1) {
  51.                     console.log('No rakiya, only JavaScript' + ' ' + startRow + ' ' + startCol);
  52.                     return;
  53.                 }
  54.                 else if(field[newPosRow][newPosCol] === 0){
  55.                     if(i === binaryPosition.length - 1){
  56.                         canMove = false;
  57.                     }
  58.                     continue;
  59.                 }
  60.                 else{
  61.                     field[startRow][startCol] = 0;
  62.                     startRow = newPosRow;
  63.                     startCol = newPosCol;
  64.                     currentPosition = field[startRow][startCol];
  65.                     //console.log('current position is: ' + field[startRow][startCol]);
  66.                     break;
  67.                 }
  68.             }
  69.         }
  70.         if(!canMove){
  71.             console.log('No JavaScript, only rakiya' +  ' ' +  startRow + ' ' + startCol);
  72.             return;
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement