Advertisement
Guest User

Goshko the rabbit

a guest
Apr 5th, 2015
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     var directions = [],
  3.         matrix = [],
  4.         matrixRow = [],
  5.         inputLine = [],
  6.         index,
  7.         startRow = 0,
  8.         startCol = 0,
  9.         currentRow = startRow,
  10.         currentCol = startCol,
  11.         direction,
  12.         i,
  13.         movedToNewCell,
  14.         cell,
  15.         eatenVegetables = {},
  16.         eatenVegetablesPrint,
  17.         cellsPassed = [];
  18.    
  19.     //parse input
  20.     for (index in input) {
  21.         inputLine = input[index].split(', ');
  22.         if (Number(index) === 0) {
  23.             directions = inputLine;
  24.         } else {
  25.             matrix.push(inputLine);
  26.         }
  27.     }
  28.    
  29.     //initialize an object to store the eaten vegetables count
  30.     eatenVegetables = {
  31.         'lettuces': 0,
  32.         'cabbages': 0,
  33.         'turnips': 0,
  34.         'carrots': 0,
  35.         'wallHits': 0
  36.     };
  37.    
  38.     //move through the matrix and eat vegetables
  39.     //move as many times as there are directions
  40.     for (i = 0; i < directions.length; i++) {
  41.         direction = directions[i];
  42.         movedToNewCell = false;
  43.         switch (direction) {
  44.             case 'up':
  45.                 if (currentRow > 0) {
  46.                     currentRow -= 1;
  47.                     movedToNewCell = true;
  48.                 } else {
  49.                     eatenVegetables.wallHits++;
  50.                 }
  51.                 break;
  52.             case 'down':
  53.                 if (currentRow < matrix.length - 1) {
  54.                     currentRow += 1;
  55.                     movedToNewCell = true;
  56.                 } else {
  57.                     eatenVegetables.wallHits++;
  58.                 }
  59.                 break;
  60.             case 'left':
  61.                 if (currentCol > 0) {
  62.                     currentCol -= 1;
  63.                     movedToNewCell = true;
  64.                 } else {
  65.                     eatenVegetables.wallHits++;
  66.                 }
  67.                 break;
  68.             case 'right':
  69.                 if (currentCol < matrix[currentRow].length - 1) {
  70.                     currentCol += 1;
  71.                     movedToNewCell = true;
  72.                 } else {
  73.                     eatenVegetables.wallHits++;
  74.                 }
  75.                 break;
  76.         }
  77.        
  78.         //check if the rabbit has moved to a new cell
  79.         //if he has, he will eat all vegetables there
  80.         //also, we must add the cell (after he has eaten everything)
  81.         //to the result to print it later
  82.        
  83.         if (movedToNewCell) {
  84.             cell = matrix[currentRow][currentCol];
  85.             var carrotsCount = (cell.match(/({!})/g) || []).length;
  86.             var cabbageCount = (cell.match(/({\*})/g) || []).length;
  87.             var lettuceCount = (cell.match(/({&})/g) || []).length;
  88.             var turnipCount = (cell.match(/({#})/g) || []).length;
  89.            
  90.             eatenVegetables.carrots += carrotsCount;
  91.             eatenVegetables.cabbages += cabbageCount;
  92.             eatenVegetables.lettuces += lettuceCount;
  93.             eatenVegetables.turnips += turnipCount;
  94.            
  95.             //replace the eaten vegetables in the cell with @
  96.             cell = cell.replace(/({!})/g, '@');
  97.             cell = cell.replace(/({\*})/g, '@');
  98.             cell = cell.replace(/({&})/g, '@');
  99.             cell = cell.replace(/({#})/g, '@');
  100.            
  101.             //add the passed cell to an array
  102.             cellsPassed.push(cell);
  103.            
  104.             //update the cell in the matrix after eating the vegetables
  105.             matrix[currentRow][currentCol] = cell;
  106.         }
  107.     }
  108.    
  109.     //create an object to print and replace the key names with the ones from the output
  110.     eatenVegetablesPrint = JSON.stringify(eatenVegetables);
  111.     eatenVegetablesPrint = eatenVegetablesPrint.replace("carrots", "!");
  112.     eatenVegetablesPrint = eatenVegetablesPrint.replace("lettuces", "&");
  113.     eatenVegetablesPrint = eatenVegetablesPrint.replace("cabbages", "*");
  114.     eatenVegetablesPrint = eatenVegetablesPrint.replace("turnips", "#");
  115.     eatenVegetablesPrint = eatenVegetablesPrint.replace("wallHits", "wall hits");
  116.    
  117.     //print the result
  118.     console.log(eatenVegetablesPrint);
  119.     if (cellsPassed.length > 0) {
  120.         console.log(cellsPassed.join('|'));
  121.     } else {
  122.         console.log('no');
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement