Created
July 14, 2012 22:35
-
-
Save jcubic/3113713 to your computer and use it in GitHub Desktop.
Candy Game challage on Hacker Rank
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(start) { | |
function send(method, data, fun) { | |
$.ajax({ | |
type: method, | |
url: '/splash/challenge.json', | |
dataType: 'json', | |
data: data, | |
success: fun | |
}); | |
} | |
function __move(count) { // number of moves | |
return count % 6; | |
} | |
var splash = $('#splash-codechecker-insert'); | |
function display(str) { | |
splash.append('<div>' + str + '</div>'); | |
splash.scrollTop(splash.prop ? splash.prop('scrollHeight') : splash.attr('scrollHeight')); | |
} | |
function init(n, finish) { | |
if (__move(n) != 0) { | |
send('post', {n:n, remote: true}, function(response) { | |
if (response.current) { | |
next_move(response.current, finish); | |
} | |
}); | |
} else { | |
display(n + ' skip'); | |
finish(); | |
} | |
} | |
function next_move(n, finish) { | |
var move = __move(n); | |
move = move || 5; | |
send('put', {move: move, remote: true}, function(response) { | |
if (response.game.current !== 0) { | |
setTimeout(function() { | |
next_move(response.game.current, finish); | |
}, 0); | |
} else { | |
display(response.game.n + ' ' + (response.game.solved ? 'done' : 'fail')); | |
finish(); | |
} | |
}); | |
} | |
function iterate(n) { | |
init(n, function() { | |
if (n < 2560) { | |
setTimeout(function() { | |
iterate(++n); | |
}, 0); | |
} | |
}); | |
} | |
iterate(start); | |
})(7) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment