Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(args) {
- function offRange(matrix, row, col) {
- var rowss = matrix.length;
- var colss = matrix[0].length;
- return row<0 || col<0 || row>rowss-1 || col>colss-1;
- }
- function matrix(rows, cols) {
- value = false;
- var arr = [];
- for (var i = 0; i < rows; i++) {
- arr.push([]);
- arr[i].push(new Array(cols));
- for (var j = 0; j < cols; j++) {
- arr[i][j] = value;
- }
- }
- return arr;
- }
- function getMovement(dir) {
- switch(dir) {
- case '1':
- return directions.one;
- break;
- case '2':
- return directions.two;
- break;
- case '3':
- return directions.three;
- break;
- case '4':
- return directions.four;
- break;
- case '5':
- return directions.five;
- break;
- case '6':
- return directions.six;
- break;
- case '7':
- return directions.seven;
- break;
- case '8':
- return directions.eight;
- break;
- }
- }
- var dimentions = args[0].split(' ');
- var rows = parseInt(dimentions[0]);
- var cols = parseInt(dimentions[1]);
- var visited = matrix(rows, cols);
- var moves = args.slice(1);
- var row = visited.length - 1;
- var col = visited[0].length - 1;
- var directions = {
- one: { row: -2, col: 1},
- two: { row: -1, col: 2},
- three: { row: 1, col: 2},
- four: { row: 2, col: 1},
- five: { row: 2, col: -1},
- six: { row: 1, col: -2},
- seven: { row: -1, col: -2},
- eight: { row: -2, col: -1}
- };
- var sum = 0;
- var move = 0;
- while (true) {
- if (offRange(visited, row, col)) {
- return 'Go go Horsy! Collected ' + sum +' weeds' ;
- }
- if (visited[row][col]) {
- return 'Sadly the horse is doomed in ' + move +' jumps';
- }
- sum += Math.pow(2,row) - col;
- move += 1;
- visited[row][col] = true;
- var dir = moves[row][col];
- //console.log(moves[row][col]);
- var goTo = getMovement(dir);
- row += goTo.row;
- col += goTo.col;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment