Advertisement
Guest User

Horsy

a guest
Jul 21st, 2014
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Horsy</title>
  6. </head>
  7. <body>
  8. <script>
  9. function Solve(args) {
  10.     var rowsCols = args[0].split(' '),
  11.         matrixRows = parseInt(rowsCols[0]),
  12.         matrixCols = parseInt(rowsCols[1]),
  13.         directions = [],
  14.         numbers = [],
  15.         totalSum = 0,
  16.         totalJumps = 0,
  17.         counter;
  18.  
  19.     for (var i = 1; i < args.length; i++) {
  20.         var currentRow = args[i];
  21.         directions[i-1] = [];
  22.         for (var j = 0; j < currentRow.length; j++) {
  23.             directions[i-1].push(parseInt(currentRow[j]));
  24.         }
  25.     }
  26.  
  27.     for (var rows = 0; rows < matrixRows; rows++) {
  28.         numbers[rows] = [];
  29.         counter = Math.pow(2, rows);
  30.         for (var cols = 0; cols < matrixCols; cols++) {
  31.             numbers[rows].push(counter);
  32.             counter -= 1;
  33.         }
  34.     }
  35.  
  36.     var jumpRow = matrixRows - 1;
  37.     var jumpCol = matrixCols - 1;
  38.  
  39.     while (true) {
  40.  
  41.         if (jumpRow < 0 || jumpRow >= matrixRows ||
  42.             jumpCol < 0 || jumpCol >= matrixCols) {
  43.             return 'Go go Horsy! Collected ' +
  44.                     totalSum + ' weeds';
  45.         } else if (numbers[jumpRow][jumpCol] === 'visited') {
  46.             return 'Sadly the horse is doomed in ' +
  47.                     totalJumps + ' jumps';
  48.         } else {
  49.             totalSum += numbers[jumpRow][jumpCol];
  50.             totalJumps += 1;
  51.             numbers[jumpRow][jumpCol] = 'visited';
  52.         }
  53.  
  54.         switch (directions[jumpRow][jumpCol]) {
  55.             case 1:
  56.                 jumpRow -= 2;
  57.                 jumpCol += 1;
  58.                 break;
  59.             case 2:
  60.                 jumpRow -= 1;
  61.                 jumpCol += 2;
  62.                 break;
  63.             case 3:
  64.                 jumpRow += 1;
  65.                 jumpCol += 2;
  66.                 break;
  67.             case 4:
  68.                 jumpRow += 2;
  69.                 jumpCol += 1;
  70.                 break;
  71.             case 5:
  72.                 jumpRow += 2;
  73.                 jumpCol -= 1;
  74.                 break;
  75.             case 6:
  76.                 jumpRow += 1;
  77.                 jumpCol -= 2;
  78.                 break;
  79.             case 7:
  80.                 jumpRow -= 1;
  81.                 jumpCol -= 2;
  82.                 break;
  83.             case 8:
  84.                 jumpRow -= 2;
  85.                 jumpCol -= 1;
  86.         }
  87.     }
  88. }
  89.  
  90. var input = [
  91.     '3 5',
  92.   '54361',
  93.   '43326',
  94.   '52888'
  95. ];
  96.  
  97. console.log(Solve(input));
  98. </script>
  99. </body>
  100. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement