Skip to content

Instantly share code, notes, and snippets.

@Palmr
Created September 26, 2014 15:34
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 Palmr/f495eba47514cc152c51 to your computer and use it in GitHub Desktop.
Save Palmr/f495eba47514cc152c51 to your computer and use it in GitHub Desktop.
JS solution for the challenege on https://trello.com/jobs/developer
// for some reason the challenege only let you use these letters, not full A-Z
var letters = "acdegilmnoprstuw";
// Implementation of their hash function to test against
function doHash(string) {
var hash = 7;
for(var i = 0; i < string.length; i++) {
hash = (hash * 37 + letters.indexOf(string[i]));
}
return hash;
}
// Code to work back, one letter at a time, until a valid string is found for the hash
function testChar(currentLetter, hash, partialMatch) {
var x = (hash - letters.indexOf(currentLetter)) / 37;
if (Math.floor(x) - x == 0.0) {
if (x == 7) { // Their hash starts with 7 so if we found this int, we're done
console.log('Match Found: ' + currentLetter + partialMatch);
return true;
}
// If the currentLetter was valid then recurse down with other letter guesses
for (var i = 0; i < letters.length; i++) {
testChar(letters[i], x, currentLetter + partialMatch);
}
}
else {
// If we didn't get an int after working back then the currentLetter wasn't valid
return false;
}
}
// Break a hash
function breakHash(hash) {
for (var i = 0; i < letters.length; i++) {
testChar(letters[i], hash, '');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment