Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- var directions = [],
- matrix = [],
- matrixRow = [],
- inputLine = [],
- index,
- startRow = 0,
- startCol = 0,
- currentRow = startRow,
- currentCol = startCol,
- direction,
- i,
- movedToNewCell,
- cell,
- eatenVegetables = {},
- eatenVegetablesPrint,
- cellsPassed = [];
- //parse input
- for (index in input) {
- inputLine = input[index].split(', ');
- if (Number(index) === 0) {
- directions = inputLine;
- } else {
- matrix.push(inputLine);
- }
- }
- //initialize an object to store the eaten vegetables count
- eatenVegetables = {
- 'lettuces': 0,
- 'cabbages': 0,
- 'turnips': 0,
- 'carrots': 0,
- 'wallHits': 0
- };
- //move through the matrix and eat vegetables
- //move as many times as there are directions
- for (i = 0; i < directions.length; i++) {
- direction = directions[i];
- movedToNewCell = false;
- switch (direction) {
- case 'up':
- if (currentRow > 0) {
- currentRow -= 1;
- movedToNewCell = true;
- } else {
- eatenVegetables.wallHits++;
- }
- break;
- case 'down':
- if (currentRow < matrix.length - 1) {
- currentRow += 1;
- movedToNewCell = true;
- } else {
- eatenVegetables.wallHits++;
- }
- break;
- case 'left':
- if (currentCol > 0) {
- currentCol -= 1;
- movedToNewCell = true;
- } else {
- eatenVegetables.wallHits++;
- }
- break;
- case 'right':
- if (currentCol < matrix[currentRow].length - 1) {
- currentCol += 1;
- movedToNewCell = true;
- } else {
- eatenVegetables.wallHits++;
- }
- break;
- }
- //check if the rabbit has moved to a new cell
- //if he has, he will eat all vegetables there
- //also, we must add the cell (after he has eaten everything)
- //to the result to print it later
- if (movedToNewCell) {
- cell = matrix[currentRow][currentCol];
- var carrotsCount = (cell.match(/({!})/g) || []).length;
- var cabbageCount = (cell.match(/({\*})/g) || []).length;
- var lettuceCount = (cell.match(/({&})/g) || []).length;
- var turnipCount = (cell.match(/({#})/g) || []).length;
- eatenVegetables.carrots += carrotsCount;
- eatenVegetables.cabbages += cabbageCount;
- eatenVegetables.lettuces += lettuceCount;
- eatenVegetables.turnips += turnipCount;
- //replace the eaten vegetables in the cell with @
- cell = cell.replace(/({!})/g, '@');
- cell = cell.replace(/({\*})/g, '@');
- cell = cell.replace(/({&})/g, '@');
- cell = cell.replace(/({#})/g, '@');
- //add the passed cell to an array
- cellsPassed.push(cell);
- //update the cell in the matrix after eating the vegetables
- matrix[currentRow][currentCol] = cell;
- }
- }
- //create an object to print and replace the key names with the ones from the output
- eatenVegetablesPrint = JSON.stringify(eatenVegetables);
- eatenVegetablesPrint = eatenVegetablesPrint.replace("carrots", "!");
- eatenVegetablesPrint = eatenVegetablesPrint.replace("lettuces", "&");
- eatenVegetablesPrint = eatenVegetablesPrint.replace("cabbages", "*");
- eatenVegetablesPrint = eatenVegetablesPrint.replace("turnips", "#");
- eatenVegetablesPrint = eatenVegetablesPrint.replace("wallHits", "wall hits");
- //print the result
- console.log(eatenVegetablesPrint);
- if (cellsPassed.length > 0) {
- console.log(cellsPassed.join('|'));
- } else {
- console.log('no');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement