vonko1988

JSFormulaBit

May 6th, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Solver(params) {
  2.     //var params = [2, 38, 20, 48, 111, 15, 3, 43];
  3.  
  4.     var matrix = [];
  5.  
  6.     for (var row = 0; row < 8; row++) {
  7.         matrix[row] = new Array;
  8.  
  9.         for (var col = 0; col < 8; col++) {
  10.             var bit = (params[row] >> (7 - col)) & 1;
  11.             matrix[row][col] = parseInt(bit);
  12.             //process.stdout.write(matrix[row][col] + " ");
  13.         }
  14.         //console.log();
  15.     }
  16.  
  17.     var x = 7;
  18.     var y = 0;
  19.     var upOrDownCounter = 0;
  20.     var countSteps = 1;
  21.     var numberTurns = 0;
  22.     var countTurns = 0;
  23.     var direction = "down";
  24.  
  25.     for (var steps = 0; steps < 8 * 8; steps++) {
  26.  
  27.         switch (direction) {
  28.  
  29.             case "down":
  30.                 if ((y + 1 <= 7) && (matrix[y + 1][x] === 0)) {
  31.                     y++;
  32.                     countSteps++;
  33.                 }
  34.                 else if ((x - 1 >= 0) && (matrix[y + 1][x] == 1) && (matrix[y][x - 1] == 0)) {
  35.                     x--;
  36.                     countSteps++;
  37.                     upOrDownCounter++;
  38.                     numberTurns++;
  39.                     direction = "left";
  40.                 }
  41.                 else if ((y == 7) && (x-1 >=0) && (matrix[y][x - 1] == 0)) {
  42.                     x--;
  43.                     countSteps++;
  44.                     upOrDownCounter++;
  45.                     numberTurns++;
  46.                     direction = "left";
  47.                 }
  48.                 else {
  49.                     break;
  50.                 }
  51.                 break;
  52.  
  53.             case "left":
  54.                 if ((x - 1 >= 0) && (matrix[y][x - 1] == 0)) {
  55.                     x--;
  56.                     countSteps++;
  57.                 }
  58.                 else if ((x == 0) || (matrix[y][x - 1] == 1)) {
  59.                     if ((upOrDownCounter % 2 == 1) && (y-1 >=0) && (matrix[y - 1][x] == 0)) {
  60.                         y--;
  61.                         countSteps++;
  62.                         numberTurns++;
  63.                         direction = "up";
  64.                     }
  65.                     else if ((upOrDownCounter % 2 == 0) && (y + 1 <= 7) && (matrix[y + 1][x] == 0)) {
  66.                         y++;
  67.                         countSteps++;
  68.                         numberTurns++;
  69.                         direction = "down";
  70.                     }
  71.                     else {
  72.                         break;
  73.                     }
  74.                 }
  75.                 else {
  76.                     break;
  77.                 }
  78.                 break;
  79.  
  80.             case "up":
  81.                 if ((y - 1 >= 0) && (matrix[y - 1][x] == 0)) {
  82.                     y--;
  83.                     countSteps++;
  84.                 }
  85.                 else if ((y - 1 >= 0) && (matrix[y - 1][x] == 1) && (x - 1 >= 0) && (matrix[y][x - 1] == 0)) {
  86.                     x--;
  87.                     countSteps++;
  88.                     upOrDownCounter++;
  89.                     numberTurns++;
  90.                     direction = "left";
  91.                 }
  92.                 else if ((y == 0) && (x-1 >=0) && (matrix[y][x - 1] == 0)) {
  93.                     x--;
  94.                     countSteps++;
  95.                     upOrDownCounter++;
  96.                     numberTurns++;
  97.                     direction = "left";
  98.                 }
  99.                 else {
  100.                     break;
  101.                 }
  102.         }
  103.     }
  104.  
  105.     //console.log("turns: " + numberTurns);
  106.     //console.log("direction: " + direction);
  107.     //console.log("UpOrDOwn: " + upOrDownCounter);
  108.     //console.log("X: " + x);
  109.     //console.log("Y: " + y);
  110.     //console.log("Steps: " + countSteps);
  111.  
  112.     if (x == 0 && y == 7) {
  113.         return (countSteps + " " + numberTurns);
  114.     }
  115.     else {
  116.         return ("No " + countSteps);
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment