Solve the New York Times Spelling Bee
PowerShell, 50 44 41 39 43 bytes
param($a,$m)gc z|sls $m|sls "^[$a$m]{5,9}$"
Notes
- Using a
scriptblock (unnamed function) because there's still a requirement for a functionfull program. - Takes the "allowed" letters as a single string.
- Using The English Open Word List which is provided as a series of lists (one for each letter). I've combined them all into a single file named
z
,but I only included words that are between 5 and 9 letters, inclusivedue to the new rule, the wordlist and my gist have been updated to not exclude any of the original words (of any size). Here's the list, for anyone else to use.
Explanation
Reads the wordlist file z
as lines and does two regular expression matches (by way of Select-String
with the sls
alias). The first sls
matches the mandatory letter, so the result is all the words that contain the letter, then that gets piped into the second sls
which uses an expression that, in the case of the example input, looks like this: ^[acdhirn]{5,9}$
(so it matches only words that consist of those letters and no others, between 5 and 9 characters in length, inclusive).
Invocation and Output
&{param($a,$m)gc z|sls $m|sls ^[$a$m]+$} 'acdhir' 'n'
Output (57 words):
acarian
acaridan
acini
ahind
anana
anarch
anarchic
arachnid
arcana
arnica
cairn
canard
cancan
candid
candida
canid
canna
cannach
caranna
carina
chain
characin
chicana
china
chinar
chinch
cinch
circadian #
cnida
cnidarian
cranch
crania
dharna
diarian
dinar
dinic
drain
handcar
harridan #
inarch
indican
indicia
indri
iridian
nadir
naiad
naira
nanna
niacin #
nicad
rachidian
radian
ranarian
ranch
rancid
randan
ricin
Mathematica, 143 130 bytes
Join@@StringCases[Join@@StringCases[WordList[],RegularExpression["^["<>#2<>#<>"]{5,9}$"]],RegularExpression["\w+(?="<>#<>")\w+"]]&
Invocation (with input)
Join@@StringCases[Join@@StringCases[WordList[],RegularExpression["^["<>#2<>#<>"]{5,9}$"]],RegularExpression["\w+(?="<>#<>")\w+"]]&["n","acdhir"]
Output (20 words)
{"anarchic", "arachnid", "cairn", "canard", "cancan", "candid", "candida", "chain", "china", "cinch", "circadian", "cnidarian", "dinar", "drain", "handcar", "harridan", "niacin", "radian", "ranch", "rancid"}
Explanation
Join@@ // Shortened version of Flatten; removes {} from StringCases lists
StringCases[ // Find substrings in a string/list that match a regex pattern.
Join@@
StringCases[
WordList[], // Built-in function; returns a list of English words.
RegularExpression[
"^["<>#2<>#<>"]{5,9}$" // Take the WordList, and find 5-9 letter
// words with any character in the
// the first or second arguments.
// In this case, "x" would be "achdir",
// and "y" is the letter "n".
// <> is for string concatenation.
]
],
RegularExpression[
"\w+(?="<>#<>")\w+" // Take the result from the previous
// StringCases function, and find words
// that actually have the character(s) in
// the second argument in them.
// In this case, find words that actually
// have the letter "n" in them.
]
]
& // Define an anonymous function.
["n","acdhir"] // Pass arguments to the function; "#" is "n", and "#2" is "acdhir".
bash/dash/ash with grep and egrep, 31 30 29 bytes
egrep ^[$1$2]\{5,}$ z|grep $2
This assumes you have a a word list in a file named z
. The word list I used was kindly provided by briantist.
Example:
$ echo 'egrep ^[$1$2]\{5,}$ z|grep $2' > program; chmod +x program
$ ./program acdhir n | tr '\n' ' '
acarian acaridan acini ahind anana anarch anarchic arachnid arachnidan arcana arnica cairn canard cancan candid candida canid canna cannach caranna carina chain characin chicana china chinar chinch cinch circadian cnida cnidarian cranch crania dharna diarian dinar dinic drain handcar harridan inarch indican indicia indri iridian nadir naiad naira nanna niacin nicad rachidian radian ranarian ranch rancid randan ricin