Skip to content

Instantly share code, notes, and snippets.

@jcubic
Created July 2, 2012 21:12
Show Gist options
  • Save jcubic/3035722 to your computer and use it in GitHub Desktop.
Save jcubic/3035722 to your computer and use it in GitHub Desktop.
Bot that hack hackerrank interface to solve game
(function() {
function enter(element, string) {
var prompt = $(element);
var press = jQuery.Event("keydown");
press.which = 13;
press.keyCode = 13;
prompt.val(string).trigger(press);
}
function move(count) { // number of moves
if (count < 12) {
return count < 6 ? count : count-6;
} else {
return move(count-6);
}
}
function get_current() {
return parseInt($('#game-output div:last').html().match(/Remaining candies: '([0-9]+)'/)[1]);
}
function solve_game(finish) {
var to_go = get_current();
enter('#game-input', move(to_go));
setTimeout(function() {
if ($('#game-output').length == 0) {
finish();
} else {
solve_game(finish);
}
}, 1000);
}
function solve(number, finish) {
var first = move(number);
if (first != 0) {
enter('#prompt-input', "challenge " + number);
setTimeout(function() {
enter('#game-input', first);
setTimeout(function() {
solve_game(finish);
}, 1000);
}, 1000);
} else {
finish();
}
}
(function iterate(n) { solve(n, function() { iterate(++n); }); })(7);
})()
@mhjortholt
Copy link

Hello

Did you get your enter-function to work? I tried the same approach as you but .trigger() never posted the input-string. I ran this with Greasemonkey. How do you run it?

@jcubic
Copy link
Author

jcubic commented Jul 4, 2012

@mhjortholt it work but it need to be keydown not keypress. And I run this script only for new games, then I wrote ajax based script which was much faster. I run from google inspector console.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment