70 lines
2.2 KiB
Text
70 lines
2.2 KiB
Text
local letters = {"d", "e", "e", "g", "k", "l", "n", "o","w"}
|
|
local word_list = "unixdict.txt" -- local copy
|
|
local nl = os.platform == "windows" ? "\r\n" : "\n"
|
|
local words = io.contents(word_list):rstrip():split(nl)
|
|
-- Get rid of words under 3 letters or over 9 letters.
|
|
words = words:filter(|w| -> 2 < #w < 10):reorder()
|
|
local found = {}
|
|
for words as word do
|
|
if "k" in word then
|
|
local lets = letters:clone()
|
|
local ok = true
|
|
for i = 1, #word do
|
|
local c = word[i]
|
|
local ix = lets:findindex(|e| -> e == c)
|
|
if !ix then
|
|
ok = false
|
|
break
|
|
end
|
|
lets:remove(ix)
|
|
end
|
|
if ok then found:insert(word) end
|
|
end
|
|
end
|
|
|
|
print($"The following {#found} words are the solutions to the puzzle:")
|
|
print(found:concat(nl))
|
|
|
|
-- Optional extra.
|
|
local most_found = 0
|
|
local most_words9 = {}
|
|
local most_letters = {}
|
|
-- Iterate through all 9 letter words in the dictionary.
|
|
for words:filtered(|w| -> #w == 9) as word9 do
|
|
letters = word9:split(""):sort()
|
|
-- Get distinct letters.
|
|
local distinct_letters = letters:dedup():reorder()
|
|
-- Place each distinct letter in the middle and see what we can do with the rest.
|
|
for distinct_letters as letter do
|
|
found = 0
|
|
for words as word do
|
|
if word:find(letter, 1, true) then
|
|
local lets = letters:clone()
|
|
local ok = true
|
|
for i = 1, #word do
|
|
local c = word[i]
|
|
local ix = lets:findindex(|e| -> e == c)
|
|
if !ix then
|
|
ok = false
|
|
break
|
|
end
|
|
lets:remove(ix)
|
|
end
|
|
if ok then found += 1 end
|
|
end
|
|
end
|
|
if found > most_found then
|
|
most_found = found
|
|
most_words9 = {word9}
|
|
most_letters = {letter}
|
|
elseif found == most_found then
|
|
most_words9:insert(word9)
|
|
most_letters:insert(letter)
|
|
end
|
|
end
|
|
end
|
|
print($"\nMost words found = {most_found}")
|
|
print("Nine letter words producing this total:")
|
|
for i = 1, #most_words9 do
|
|
print($"{most_words9[i]} with central letter '{most_letters[i]}'")
|
|
end
|