Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 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.
- 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.
- 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. */
- function Solve(args) {
- var circle = args[0].split(", ");
- for (key in circle) {
- circle[key] = parseInt(circle[key]);
- }
- var length = circle.length;
- var score = 0;
- var bestScore = 0;
- var currentPosition = 0;
- var nextPosition = 0;
- for (var step = 1; step <= length; step++) {
- for (var startPos = 0; startPos < length; startPos++) {
- var visited = {};
- score = 0;
- nextPosition = startPos;
- do {
- //current position
- if (visited[nextPosition] == undefined) {
- currentPosition = nextPosition;
- visited[currentPosition] = true;
- score++;
- } else {
- break;
- }
- //next position
- if (currentPosition + step >= length) {
- nextPosition = currentPosition + step - length;
- } else {
- nextPosition = currentPosition + step;
- }
- } while (circle[currentPosition] < circle[nextPosition])
- //score
- if (score > bestScore) {
- bestScore = score;
- }
- }
- }
- return bestScore;
- }
Advertisement
Add Comment
Please, Sign In to add comment