Last active
April 26, 2019 06:30
-
-
Save yefim/c8c38d3269b67f040cf24c9fb55ef024 to your computer and use it in GitHub Desktop.
Solve the NYTimes Spelling Bee challenge - https://www.nytimes.com/puzzles/spelling-bee
This file contains 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
const wordListPath = require('word-list'); // https://github.com/sindresorhus/word-list | |
const readlines = require('gen-readlines'); // https://github.com/neurosnap/gen-readlines | |
const isSuperset = (set, subset) => { | |
for (const elem of subset) { | |
if (!set.has(elem)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
const solve = function*(letters) { | |
const requiredLetter = letters[0]; | |
const letterSet = new Set(letters); | |
for (const line of readlines.fromFile(wordListPath)) { | |
if (line.length < 4) { | |
continue; | |
} | |
const word = line.toString(); | |
const wordSet = new Set(word.split('')); | |
if (wordSet.has(requiredLetter) && isSuperset(letterSet, wordSet)) { | |
yield word; | |
} | |
} | |
}; | |
for (const solution of solve('cfaplte'.split(''))) { | |
console.log(solution); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment