Skip to content

Instantly share code, notes, and snippets.

@keksipurkki
Last active April 5, 2022 19:38
Show Gist options
  • Save keksipurkki/9d0bc20c013dad7206bdb5d46148d0b9 to your computer and use it in GitHub Desktop.
Save keksipurkki/9d0bc20c013dad7206bdb5d46148d0b9 to your computer and use it in GitHub Desktop.
Solve http://www.playtypeshift.com like a pro
const fs = require("fs");
const cartesian = (...a) => a.reduce((a, b) => a.flatMap(d => b.map(e => [d, e].flat())));
const intersection = (a, b) => new Set([...a].filter((x) => b.has(x)));
const letters = [
["n", "h", "i", "m", "w"],
["a", "h", "y", "i", "m"],
["c", "b", "x", "o", "l"],
["k", "o", "i", "u"],
["e", "s", "n"],
["g", "y", "h", "s", "d"],
];
function dictionary() {
const str = fs.readFileSync("/usr/share/dict/words").toString();
const words = str.split("\n").filter(Boolean);
return new Set(words);
}
function trials(letters) {
const words = [...cartesian(...letters)].map((x) => x.join(""));
return new Set(words);
}
function main() {
try {
console.log(intersection(dictionary(), trials(letters)));
} catch (e) {
console.error(e);
process.exit(1);
}
}
main();
Set(9) {
'hacked',
'hickey',
'macies',
'nibong',
'nickey',
'waling',
'waxing',
'whoosh',
'wicked'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment