sashomaga

Joro the Rabbit

Mar 30th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Joro is a rabbit. But he is no ordinary rabbit – he just loves to jump around. But jumping around without any precalculated direction is too ordinary, so he likes jumping in just a given direction and to make it more fun, the jumping is done in a circle. By given terrain, help Joro find longest fun and not ordinary route of jumps.
  2. You are given the terrain as sequence of numbers. The terrain should form a circle, so the last number is before the first, and the first is after the last.
  3.  
  4. Joro can enter the terrain from every position, jump only on numbers larger than the one he is on, only in direction left-to-right and with the same step. Joro’s jumping steps range from 1 to the size of the terrain. Joro cannot jump on position that he already visited. */
  5.  
  6. function Solve(args) {
  7.  
  8.         var circle = args[0].split(", ");
  9.         for (key in circle) {
  10.             circle[key] = parseInt(circle[key]);
  11.         }
  12.         var length = circle.length;
  13.         var score = 0;
  14.         var bestScore = 0;
  15.         var currentPosition = 0;
  16.         var nextPosition = 0;
  17.         for (var step = 1; step <= length; step++) {
  18.  
  19.             for (var startPos = 0; startPos < length; startPos++) {
  20.  
  21.                 var visited = {};
  22.                 score = 0;
  23.                 nextPosition = startPos;
  24.                 do {
  25.                     //current position                  
  26.                     if (visited[nextPosition] == undefined) {
  27.                         currentPosition = nextPosition;
  28.                         visited[currentPosition] = true;
  29.                         score++;
  30.                     } else {
  31.                         break;
  32.                     }
  33.                     //next position
  34.                     if (currentPosition + step >= length) {
  35.                         nextPosition = currentPosition + step - length;
  36.                     } else {
  37.                         nextPosition = currentPosition + step;
  38.                     }
  39.                    
  40.                 } while (circle[currentPosition] < circle[nextPosition])
  41.                 //score
  42.                 if (score > bestScore) {
  43.                     bestScore = score;
  44.                 }                
  45.             }
  46.         }
  47.         return bestScore;
  48.     }
Advertisement
Add Comment
Please, Sign In to add comment