Skip to content

Instantly share code, notes, and snippets.

@toobulkeh
Last active January 11, 2017 18:56
Show Gist options
  • Save toobulkeh/c59e2ad178a9d01df99f13f7aa667f8b to your computer and use it in GitHub Desktop.
Save toobulkeh/c59e2ad178a9d01df99f13f7aa667f8b to your computer and use it in GitHub Desktop.
trelloreversehash
//https://trello.com/jobs/details?gh_jid=480058
window.letters = "acdegilmnoprstuw";
function hash(s) {
var h = 7;
for(var i = 0; i < s.length; i++) {
h = h * 37 + letters.indexOf(s[i]);
}
return h;
}
//this is O(letters.length ^ left). It'll crash your shit.
function brutehash(s, left) {
if(left === 0) {
if(hash(s) === 680131659347) {
console.log('found it: ' + s);
return true;
} else {
return false;
}
} else {
for(var i = 0; i < letters.length; i++) {
if(brutehash(s + letters[i], left-1)) {
return true;
}
}
return false;
}
}
//reversehash(930846109532517, '') = 'found: lawnmower'
//reversehash(680131659347, '') = 'found: leepadg'
function reversehash(num, result) {
for(var i = 0; i < letters.length; i++) {
rev = (num - i) / 37;
//console.log('trying '+letters[i]+', rev: '+rev);
if(rev % 1 === 0) {
result = letters[i] + result;
if(rev === 7) {
console.log('found: '+result);
return true;
} else {
if(reversehash(rev, result)) {
return true;
}
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment