Skip to content

Instantly share code, notes, and snippets.

@framp
Last active August 29, 2015 14:16
Show Gist options
  • Save framp/d74fc7dd9886f49d52a5 to your computer and use it in GitHub Desktop.
Save framp/d74fc7dd9886f49d52a5 to your computer and use it in GitHub Desktop.
Breaking Trello challenge

Write JavaScript (or CoffeeScript) code to find a 9 letter string of characters that contains only letters from

acdegilmnoprstuw

such that the hash(the_string) is

956446786872726

if hash is defined by the following pseudo-code:

Int64 hash (String s) {
    Int64 h = 7
    String letters = "acdegilmnoprstuw"
    for(Int32 i = 0; i < s.length; i++) {
        h = (h * 37 + letters.indexOf(s[i]))
    }
    return h
}

For example, if we were trying to find the 7 letter string where hash(the_string) was 680131659347, the answer would be "leepadg".

//Same solution in JavaScript
(function() {
var solve = function(base, set, hash, code){
return [Math.floor(hash/base), set[hash%base] + code]
};
var solvePuzzle = solve.bind(null, 37, "acdegilmnoprstuw");
var puzzle = [956446786872726, ''], iterations = 9;
while(iterations--)
puzzle = solvePuzzle.apply(null, puzzle);
console.log(puzzle);
})();
Every number returned by the `hash` function is given by the sum
of a number `x` where `mod x 37 == 0` and a number `x <- [0..15]`
The `solve` function calculates the next number to solve
and add the current letter to the solution
> solve base set (hash, code) = (div hash base, set !! mod hash base : code)
The `solvePuzzle` function defines the parameter of the puzzle
> solvePuzzle = solve 37 "acdegilmnoprstuw"
The `main` function iterates on `solve` 9 times with hash as the starting value
> main = print (iterate solvePuzzle (956446786872726, "") !! 9)
@framp
Copy link
Author

framp commented Mar 11, 2015

Adding an extra Haskell solution because, at 3LOC is pretty damn expressive and concise

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