Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Horsy</title>
- </head>
- <body>
- <script>
- function Solve(args) {
- var rowsCols = args[0].split(' '),
- matrixRows = parseInt(rowsCols[0]),
- matrixCols = parseInt(rowsCols[1]),
- directions = [],
- numbers = [],
- totalSum = 0,
- totalJumps = 0,
- counter;
- for (var i = 1; i < args.length; i++) {
- var currentRow = args[i];
- directions[i-1] = [];
- for (var j = 0; j < currentRow.length; j++) {
- directions[i-1].push(parseInt(currentRow[j]));
- }
- }
- for (var rows = 0; rows < matrixRows; rows++) {
- numbers[rows] = [];
- counter = Math.pow(2, rows);
- for (var cols = 0; cols < matrixCols; cols++) {
- numbers[rows].push(counter);
- counter -= 1;
- }
- }
- var jumpRow = matrixRows - 1;
- var jumpCol = matrixCols - 1;
- while (true) {
- if (jumpRow < 0 || jumpRow >= matrixRows ||
- jumpCol < 0 || jumpCol >= matrixCols) {
- return 'Go go Horsy! Collected ' +
- totalSum + ' weeds';
- } else if (numbers[jumpRow][jumpCol] === 'visited') {
- return 'Sadly the horse is doomed in ' +
- totalJumps + ' jumps';
- } else {
- totalSum += numbers[jumpRow][jumpCol];
- totalJumps += 1;
- numbers[jumpRow][jumpCol] = 'visited';
- }
- switch (directions[jumpRow][jumpCol]) {
- case 1:
- jumpRow -= 2;
- jumpCol += 1;
- break;
- case 2:
- jumpRow -= 1;
- jumpCol += 2;
- break;
- case 3:
- jumpRow += 1;
- jumpCol += 2;
- break;
- case 4:
- jumpRow += 2;
- jumpCol += 1;
- break;
- case 5:
- jumpRow += 2;
- jumpCol -= 1;
- break;
- case 6:
- jumpRow += 1;
- jumpCol -= 2;
- break;
- case 7:
- jumpRow -= 1;
- jumpCol -= 2;
- break;
- case 8:
- jumpRow -= 2;
- jumpCol -= 1;
- }
- }
- }
- var input = [
- '3 5',
- '54361',
- '43326',
- '52888'
- ];
- console.log(Solve(input));
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement