Skip to content

Instantly share code, notes, and snippets.

@yefim
Last active April 26, 2019 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yefim/c8c38d3269b67f040cf24c9fb55ef024 to your computer and use it in GitHub Desktop.
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
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