Skip to content

Instantly share code, notes, and snippets.

@justinmarsan
Last active December 19, 2015 08:29
Show Gist options
  • Save justinmarsan/5925570 to your computer and use it in GitHub Desktop.
Save justinmarsan/5925570 to your computer and use it in GitHub Desktop.
// possible words
var words = ["lazy", "crackers", "hello", "cruel", "world", "bears", "need", "not", "apply"];
// characters
var puzzle = " \
r b f x w \
h e l l o \
o a h c r \
c r u e l \
v s u f d \
";
var solve = function solve(puzzle, words) {
var results = [],
puzzle = puzzle.replace(/\s/g, ''),
lineCount = Math.round(Math.sqrt(puzzle.length)) - 1;
for(var _i = 0, _len = words.length; _i < _len; _i++) {
var w = words[_i],
re = '('+w+')|('+w.split('').join('.{'+ lineCount +'}')+')';
puzzle.match(RegExp(re, 'g')) && results.push(w);
}
return results;
};
solve(puzzle, words);
//=> ["hello", "cruel", "world", "bears"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment