Created
October 10, 2015 19:49
-
-
Save gartenfeld/4065dd67ad315380eacf to your computer and use it in GitHub Desktop.
Solving a puzzle with async depth-first traversal.
This file contains hidden or 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
| var request = require('request'); | |
| var Promise = require('bluebird'); | |
| var authorize = require('./session-keys'); | |
| var BASE_URL = 'http://challenge.shopcurbside.com/'; | |
| var fetch = function(resource, session) { | |
| var options = { | |
| url: BASE_URL + resource, | |
| headers: { 'Session': session } | |
| }; | |
| return new Promise(function(resolve, reject) { | |
| request(options, function(err, res, body) { | |
| resolve(JSON.parse(body || [])); | |
| }); | |
| }); | |
| }; | |
| var Tree = function(id) { | |
| this.id = id; | |
| this.value = null; | |
| this.children = []; | |
| }; | |
| var stack = 0; | |
| // `node` is the JSON object from Curbside | |
| // `mirror` represents that node on the local sync tree | |
| var traverse = function(node, mirror) { | |
| mirror.value = node.secret || null; | |
| var clues = node.next || node.nExt || node.neXT || []; | |
| if (typeof clues === 'string') { clues = [clues]; } | |
| clues.forEach(function(clue) { | |
| var child = new Tree(clue); | |
| mirror.children.push(child); | |
| stack++; | |
| authorize() | |
| .then(function(session) { | |
| return fetch(clue, session); | |
| }) | |
| .then(function(node) { | |
| stack--; | |
| traverse(node, child); | |
| if (stack === 0) { decipher(); } | |
| }); | |
| }); | |
| }; | |
| // this is a sync representation of the puzzle's data | |
| var puzzle; | |
| var DFT = function(node, callback) { | |
| for (var i = 0; i < node.children.length; i++) { | |
| DFT(node.children[i], callback); | |
| } | |
| callback(node); | |
| }; | |
| // a thin wrapper for DFT so it's easier to call | |
| // inside the async traversal when stack finishes | |
| var decipher = function() { | |
| var plain = ''; | |
| DFT(puzzle, function(node) { | |
| plain += node.value || ''; | |
| }); | |
| console.log(plain); | |
| }; | |
| // ACTION! | |
| authorize() | |
| .then(function(session){ | |
| return fetch('start', session); | |
| }) | |
| .then(function(root) { | |
| puzzle = new Tree(root.id); | |
| traverse(root, puzzle); | |
| }); |
This file contains hidden or 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
| var request = require('request'); | |
| var Promise = require('bluebird'); | |
| var LIMIT = 10; | |
| var copies = Array.apply(null, Array(LIMIT)); | |
| var BASE_URL = 'http://challenge.shopcurbside.com/'; | |
| var url = BASE_URL + 'get-session'; | |
| var pool = []; | |
| module.exports = function() { | |
| return new Promise(function(resolve, reject) { | |
| if (pool.length) { | |
| resolve(pool.shift()); | |
| } else { | |
| request({ url: url }, function(err, res, body) { | |
| if (!body) { err = 'No data returned.'; } | |
| if (err) { | |
| reject(err); | |
| } else { | |
| pool = copies.map(function(){ return body; }); | |
| resolve(pool.shift()); | |
| } | |
| }); | |
| } | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment