Last active
December 19, 2015 08:29
-
-
Save justinmarsan/5925570 to your computer and use it in GitHub Desktop.
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
// 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