Skip to content

Instantly share code, notes, and snippets.

@0mp
Created November 1, 2016 17:40
Show Gist options
  • Save 0mp/1b387ca648ef9ec60900609167bf1d6b to your computer and use it in GitHub Desktop.
Save 0mp/1b387ca648ef9ec60900609167bf1d6b to your computer and use it in GitHub Desktop.
An agent for solving the API Bunny 2015 mazing riddles.
var request = require('request');
var readline = require('readline');
var colors = require('colors');
var _ = require('underscore');
var links = [];
var visited = {};
var exitLink = "";
var winningLink = "";
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function askAPI(href) {
console.log("=========================================".blue);
console.log(('Number of links in the queue: ' + links.length).blue);
console.log('Connecting to: http://apibunny.com' + href );
visited[href] = true;
request.get('http://apibunny.com' + href, function(err, res, body) {
var response = JSON.parse(body);
console.dir(response);
if(response.type == "exit") {
console.log("EXIT ROOM".red)
if(exitLink == ""){
console.log("––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––".red);
console.log("WINNING LINK FOUND".red);
exitLink = response._links["self"].href;
console.log("The link to the exit room: " + exitLink);
console.log("––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––".red);
}
}
if(response.type == "cell" || response.type == "start" || response.type == "exit") {
var numberOfLinks = _.size(response._links); //number of links to be explored
for(var linkName in response._links) {
numberOfLinks--;
var link = response._links[linkName];
if(linkName.substr(0,4) == "door") {
if(!visited[link.href]) {
links.push(link.href);
} else {
console.log("Already visited!".red);
}
} else if(linkName.substr(0,3) == "egg") {
collectEgg(link.href);
} else if(linkName.substr(0,3) == "win") {
return winPrint(link.href);
} else if(exploredEveryRoom(numberOfLinks, links.length)) {
askAPI(exitLink);
}
}
if(links.length) {
var link = links.pop();
setTimeout(function() {
askAPI(link);
}, 1);
}
} else {
console.log("Unknown type of a room.");
}
});
}
function exploredEveryRoom(linksToCheckInCurrentRoom, linksToBeExplored){
return (linksToCheckInCurrentRoom + linksToBeExplored == 0) ? true : false;
}
function winPrint(link){
console.log("––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––".red);
console.log("So your winning link is:".blue);
console.log(("http://appibunny.com"+link).green);
console.log("Copy/paste it into your Web browser ^_^".blue);
console.log("––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––".red);
}
function collectEgg(link){
request.get('http://apibunny.com'+link, function(err, res, body) {
console.log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++".green);
console.log(body.green);
console.log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++".green);
});
}
rl.question("Provide your start link (e.g.: /cells/IDoLoveM-0000-0010-0x86-ontyPythonxx)\n", function(answer){
askAPI(answer);
rl.close();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment