Advertisement
dimipan80

Exams - Fire the Arrows (on JavaScript)

Jan 8th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* We are given a matrix containing arrows that need to be moved. The arrows are the following: '<'
  2.  (ASCII code 60), '>' (ASCII code 62), '^' (ASCII code 94), 'v' (ASCII code 118). Respectively pointing left,
  3.  right, up and down. There are also blank spaces that are indicated by 'o' (ASCII code 111).
  4.  There will be no other characters in the matrix.
  5.  Your task is to move all arrows, one at a time, in the direction they point to until there are
  6.  no more possible moves.
  7.  The arrows should be moved in the following order: first the ones in the uppermost row and the leftmost column.
  8.  Arrows cannot move if their way is blocked by other arrows or if they reach the end of the matrix.
  9.  The only characters that will be present in the matrix will be '<', '>', '^', 'v' and 'o'.
  10.  At the first input line you will be given a number n specifying how many rows after it will follow.
  11.  At the next n lines you will be given the matrix with the arrows that need to be moved.
  12.  The output should be the new matrix with all the arrows moved to the direction they're facing. */
  13.  
  14. "use strict";
  15.  
  16. function solve(args) {
  17.     var rows = parseInt(args[0]);
  18.     var cols = args[1].length;
  19.     var countTurns = Math.max(rows, cols);
  20.  
  21.     var matrix = [];
  22.     for (var i = 1; i < args.length; i += 1) {
  23.         matrix[i - 1] = args[i].split('');
  24.     }
  25.  
  26.     for (i = 0; i < countTurns; i += 1) {
  27.         for (var row = 0; row < matrix.length; row += 1) {
  28.             for (var col = 0; col < matrix[row].length; col += 1) {
  29.                 if (matrix[row][col] != 'o') {
  30.                     movingArrowToTheNextPosition();
  31.                 }
  32.             }
  33.         }
  34.     }
  35.  
  36.     console.log(matrix);
  37.  
  38.     function movingArrowToTheNextPosition() {
  39.         var arrow = matrix[row][col];
  40.         switch (arrow) {
  41.             case 'v':
  42.                 if (row + 1 < matrix.length && matrix[row + 1][col] == 'o') {
  43.                     matrix[row + 1][col] = arrow;
  44.                     matrix[row][col] = 'o';
  45.                 }
  46.                 break;
  47.             case '^':
  48.                 if (row - 1 >= 0 && matrix[row - 1][col] == 'o') {
  49.                     matrix[row - 1][col] = arrow;
  50.                     matrix[row][col] = 'o';
  51.                 }
  52.                 break;
  53.             case '<':
  54.                 if (col - 1 >= 0 && matrix[row][col - 1] == 'o') {
  55.                     matrix[row][col - 1] = arrow;
  56.                     matrix[row][col] = 'o';
  57.                 }
  58.                 break;
  59.             case '>':
  60.                 if (col + 1 < matrix[row].length && matrix[row][col + 1] == 'o') {
  61.                     matrix[row][col + 1] = arrow;
  62.                     matrix[row][col] = 'o';
  63.                 }
  64.                 break;
  65.         }
  66.     }
  67. }
  68.  
  69. solve([
  70.     '3',
  71.     'voo<',
  72.     'oo>o',
  73.     'o^oo'
  74. ]);
  75.  
  76. solve([
  77.     '3',
  78.     'oov',
  79.     'oo<',
  80.     'oo^'
  81. ]);
  82.  
  83. solve([
  84.     '4',
  85.     'ooo<',
  86.     'oovo',
  87.     '^ooo',
  88.     'oo^o'
  89. ]);
  90.  
  91. solve([
  92.     '5',
  93.     '^voov',
  94.     'v<^<<',
  95.     'o<o<v',
  96.     'o^<^v',
  97.     '>o>^o'
  98. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement