Advertisement
dimipan80

Exams - Horsy

Dec 27th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* You are given a matrix of directions. The rows are zero-based. The possible directions are eight and
  2.  follow the horse moves from the game chess. You should generate another matrix with the same size.
  3.  The numbers in each row are consecutive and decreasing. The leftmost (first) number in each row is
  4.  a power of 2, calculated with the formula 2row where row is the number of this row and
  5.  each cell to the right on the same row is lesser with 1. Your task is to find the sum that can be found,
  6.  using the given directions and summing the numbers in each cell you step into.
  7.  If with the current direction, you step out of the matrix – print "Go go Horsy! Collected SUM weeds'",
  8.  where SUM is the collected sum. If you step on a cell that you have previously stepped in –
  9.  print "Sadly the horse is doomed in JUMPS jumps" where JUMPS is the number of cells passed.
  10.  You start always at bottom-right cell (row-1, col-1).
  11.  The first value of the input arguments you will be the numbers R and C, separated by a space
  12.  •    R is the number of rows in the matrix
  13.  •    C is the number of columns in the matrix
  14.  The next R values in the input arguments will be exactly C directions
  15.  Your function should return a single string:
  16.  •    If success, return "Go go Horsy! Collected SUM weeds", where sum is the sum of the visited cells
  17.  •    If fail, return "Sadly the horse is doomed in JUMPS jumps", where JUMPS are the number of cells from the path
  18.  of the horse.
  19.  R will be between 3 and 20, inclusive. C will be between 3 and 1024, inclusive. */
  20.  
  21. "use strict";
  22.  
  23. function solve(args) {
  24.     args[0] = args[0].split(/\s/).filter(Boolean);
  25.     var rows = parseInt(args[0][0]);
  26.     var cols = parseInt(args[0][1]);
  27.     var matrix = new Array(rows);
  28.     for (var i = 1; i < args.length; i += 1) {
  29.         matrix[i - 1] = args[i].split('').filter(Boolean);
  30.     }
  31.  
  32.     function movingToTheNextCell() {
  33.         var cellValue = matrix[row][col];
  34.         matrix[row][col] = undefined;
  35.         switch (cellValue) {
  36.             case '1':
  37.                 row += -2;
  38.                 col += 1;
  39.                 break;
  40.             case '2':
  41.                 row += -1;
  42.                 col += 2;
  43.                 break;
  44.             case '3':
  45.                 row += 1;
  46.                 col += 2;
  47.                 break;
  48.             case '4':
  49.                 row += 2;
  50.                 col += 1;
  51.                 break;
  52.             case '5':
  53.                 row += 2;
  54.                 col += -1;
  55.                 break;
  56.             case '6':
  57.                 row += 1;
  58.                 col += -2;
  59.                 break;
  60.             case '7':
  61.                 row += -1;
  62.                 col += -2;
  63.                 break;
  64.             case '8':
  65.                 row += -2;
  66.                 col += -1;
  67.                 break;
  68.         }
  69.  
  70.         return cellValue;
  71.     }
  72.  
  73.     var sum = 0;
  74.     var jumps = 0;
  75.     var row = rows - 1;
  76.     var col = cols - 1;
  77.     do {
  78.          sum += Math.pow(2, row) - col;
  79.          if (matrix[row][col]) {
  80.               movingToTheNextCell();
  81.          } else {
  82.              return console.log("Sadly the horse is doomed in %d jumps", jumps);
  83.          }
  84.          jumps += 1;
  85.     } while (row >= 0 && row < rows && col >= 0 && col < cols);
  86.  
  87.     console.log("Go go Horsy! Collected %d weeds", sum);
  88. }
  89.  
  90. solve([
  91.     '3 5',
  92.     '54561',
  93.     '43328',
  94.     '52388'
  95. ]);
  96.  
  97. solve([
  98.     '3 5',
  99.     '54361',
  100.     '43326',
  101.     '52188'
  102. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement