Skip to content

Instantly share code, notes, and snippets.

@CrossEye
Created March 4, 2022 23:34
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 CrossEye/22d0f9bacb1aca3906e33ce53c7e163e to your computer and use it in GitHub Desktop.
Save CrossEye/22d0f9bacb1aca3906e33ce53c7e163e to your computer and use it in GitHub Desktop.
// Get all potential NYT Spelling Bee puzzles agains Peter Norvig's enable1 word list.
// I know the NYT's list is different, but I don't know if it's available.
// Results written to the console, so piping to `SpellingBeePuzzles.js` or some
// such would be appropriate. Sorted by descending possible total score.
const fetch = require ('node-fetch') // version 2 for common.js
const sum = (ns) =>
ns .reduce ((a, b) => a + b, 0)
const uniques = ([... word]) => [... new Set (word)] .sort () .join ('')
const isPangram = (word) => uniques (word) .length == 7
const points = (word) =>
word .length == 4 ? 1 : word.length + (isPangram (word) ? 7 : 0)
const getMatches = (words) => (puzzle) =>
words .filter (word => word .includes (puzzle [0]) && [...word] .every (letter => puzzle .includes (letter)))
const getScore = (words) =>
sum (words .map (points))
const getResults = (words) => (puzzle) => {
const matches = getMatches (words) (puzzle)
return {
puzzle,
score: getScore (matches),
count: matches .length,
center: puzzle [0],
others: [...puzzle] .slice (1),
words: matches
}
}
const display = ({puzzle, score, count, center, others, words}) =>
`{
puzzle: '${puzzle}',
score: ${score},
count: ${count},
center: '${center}',
others: ['${others .join ("', '")}'],
pangrams: ['${words .filter (isPangram) .join ("', '")}'],
words: ['${words .join ("', '")}']
}`
const show = (results) => `[
${results .map (display) .join (',\n ')}
]`
// norvig enable1 counts
const process = (ws) => { // 172820
const words = ws .filter (w => w.length >= 4) // 171752
const pangrams = words .filter (isPangram) // 37876
const basePuzzles = [... new Set (pangrams .map (uniques))] // 15599
const puzzles = basePuzzles .flatMap ( // 109193
bp => [0, 1, 2, 3, 4, 5, 6] .map (i => [bp [i], ...bp .slice (0, i), ...bp .slice (i + 1)] .join (''))
) .sort ()
return puzzles
// .slice (0, 100) // temp - for testing
.map (getResults (words))
.sort ((a, b) => b .score - a .score)
}
// takes many minutes (did not time it)
fetch ('http://norvig.com/ngrams/enable1.txt')
.then (r => r .text())
.then (s => s .split ('\n'))
.then (process)
.then (show)
.then (console .log)
.catch (console .warn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment