Skip to content

Instantly share code, notes, and snippets.

@jcubic
Created July 14, 2012 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcubic/3113713 to your computer and use it in GitHub Desktop.
Save jcubic/3113713 to your computer and use it in GitHub Desktop.
Candy Game challage on Hacker Rank
(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