Advertisement
dimipan80

Exams - Joro the Naughty

Dec 29th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Your task is to calculate if Joro can escape from his mother, using the given sequence of jumps.
  2.  You are given a field of size N x M where the values are as follows:
  3.  On the first row the numbers are from 1 to M, on the second row – from M+1 to 2*M,
  4.  on the third – from 2*M +1 to 3*M, etc…
  5.  By given position in the field, and using the patterns given, calculate if Joro can escape from his mother.
  6.  You are also given a sequence of jumps over the field. The jumps are described with change to the row and column,
  7.  i.e. when on position (R, C) with jump (-2, 3), Joro will go to position (R-2, C+3).
  8.  When the sequence of jumps is over, Joro must start from the start of the jumps sequence.
  9.  If Joro goes outside the field, he has escaped, if Joro goes to a previously visited position, he is caught.
  10.  Each of the string represents an integer sequence. The arguments are as follows:
  11.  args[0] contains the numbers N, M and J. (J is number of jumps)
  12.  args[1] contains the star position, R and C
  13.  args[2] to args[2+J] contains the jumps.
  14.  Your method should return a single string – “escaped SUM_OF_NUMBERS" or "caught NUMBER_OF_JUMPS". */
  15.  
  16. "use strict";
  17.  
  18. function solve(args) {
  19.     var dimensions = args[0].split(/\s+/).filter(Boolean);
  20.     var rows = parseInt(dimensions[0]), cols = parseInt(dimensions[1]), j = parseInt(dimensions[2]);
  21.     var startPosition = args[1].split(/\s+/).filter(Boolean);
  22.     var row = parseInt(startPosition[0]), col = parseInt(startPosition[1]);
  23.     var directions = {};
  24.     var i;
  25.     for (i = 2; i < j + 2; i += 1) {
  26.         args[i] = args[i].split(/\s+/).filter(Boolean);
  27.         var rowValue = parseInt(args[i][0]);
  28.         var colValue = parseInt(args[i][1]);
  29.         var key = (i - 2).toString(10);
  30.         directions[key] = [rowValue, colValue];
  31.     }
  32.    
  33.     var matrix = new Array(rows);
  34.     for (i = 0; i < matrix.length; i += 1) {
  35.         matrix[i] = new Array(cols);
  36.         for (var k = 0; k < cols; k += 1) {
  37.             matrix[i][k] = true;
  38.         }
  39.     }
  40.    
  41.     var sum = 0;
  42.     var jumpsNum = 0;
  43.     do {
  44.         sum += (row * cols) + col + 1;
  45.         var index = parseInt(jumpsNum % j);
  46.         if (matrix[row][col]) {
  47.             moveToTheNextCell();
  48.         } else {
  49.             return console.log("caught " + jumpsNum);
  50.         }
  51.         jumpsNum += 1;
  52.     } while (row >= 0 && row < rows && col >= 0 && col < cols);
  53.    
  54.     console.log("escaped " + sum);
  55.    
  56.     function moveToTheNextCell() {
  57.         if (directions.hasOwnProperty(index)) {
  58.             var rowAdd = directions[index][0];
  59.             var colAdd = directions[index][1];
  60.             var cellValue = matrix[row][col];
  61.             matrix[row][col] = false;
  62.             row += rowAdd;
  63.             col += colAdd;
  64.         }
  65.        
  66.         return cellValue;
  67.     }
  68. }
  69.  
  70. solve([
  71.     '6 7 3',
  72.     '0 0',
  73.     '2 2',
  74.     '-2 2',
  75.     '3 -1'
  76. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement