archangelmihail

Horsy

May 20th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.     function offRange(matrix, row, col) {
  3.         var rowss = matrix.length;
  4.         var colss = matrix[0].length;
  5.         return row<0 || col<0 || row>rowss-1 || col>colss-1;
  6.     }
  7.  
  8.     function matrix(rows, cols) {
  9.         value = false;
  10.         var arr = [];
  11.         for (var i = 0; i < rows; i++) {
  12.             arr.push([]);
  13.             arr[i].push(new Array(cols));
  14.             for (var j = 0; j < cols; j++) {
  15.                 arr[i][j] = value;
  16.             }
  17.         }
  18.  
  19.         return arr;
  20.     }
  21.  
  22.     function getMovement(dir) {
  23.         switch(dir) {
  24.             case '1':
  25.                 return directions.one;
  26.                 break;
  27.             case '2':
  28.                 return directions.two;
  29.                 break;
  30.             case '3':
  31.                 return directions.three;
  32.                 break;
  33.             case '4':
  34.                 return directions.four;
  35.                 break;
  36.             case '5':
  37.                 return directions.five;
  38.                 break;
  39.             case '6':
  40.                 return directions.six;
  41.                 break;
  42.             case '7':
  43.                 return directions.seven;
  44.                 break;
  45.             case '8':
  46.                 return directions.eight;
  47.                 break;
  48.         }
  49.     }
  50.  
  51.     var dimentions = args[0].split(' ');
  52.     var rows = parseInt(dimentions[0]);
  53.     var cols = parseInt(dimentions[1]);
  54.    
  55.     var visited = matrix(rows, cols);
  56.     var moves = args.slice(1);
  57.  
  58.     var row = visited.length - 1;
  59.     var col = visited[0].length - 1;
  60.  
  61.     var directions = {
  62.     one: {  row: -2,    col: 1},
  63.     two: {  row: -1,    col: 2},
  64.     three: {    row: 1, col: 2},
  65.     four: { row: 2, col: 1},
  66.     five: { row: 2, col: -1},
  67.     six: {  row: 1, col: -2},
  68.     seven: {    row: -1,    col: -2},
  69.     eight: {    row: -2,    col: -1}
  70.     };
  71.  
  72.     var sum = 0;
  73.     var move = 0;
  74.    
  75.     while (true) {
  76.         if (offRange(visited, row, col)) {
  77.             return 'Go go Horsy! Collected ' + sum +' weeds' ;
  78.         }
  79.         if (visited[row][col]) {
  80.             return 'Sadly the horse is doomed in ' + move +' jumps';
  81.         }
  82.  
  83.        
  84.         sum += Math.pow(2,row) - col;
  85.         move += 1;
  86.         visited[row][col] = true;
  87.  
  88.         var dir = moves[row][col];
  89.         //console.log(moves[row][col]);
  90.         var goTo = getMovement(dir);
  91.         row += goTo.row;
  92.         col += goTo.col;
  93.        
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment