Skip to content

Instantly share code, notes, and snippets.

@selfsame
Created November 21, 2017 16:28
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 selfsame/59ac4fd3efd33b056923b0d9dc4867c0 to your computer and use it in GitHub Desktop.
Save selfsame/59ac4fd3efd33b056923b0d9dc4867c0 to your computer and use it in GitHub Desktop.
(def words (string/split (slurp "/usr/share/dict/words") #"\n"))
(def words-trie
(reduce
(fn [trie word]
(assoc-in trie (vec (cons (count word) word)) {}))
{} words))
(defn keys-in [tree]
(if (map? tree)
(vec
(mapcat
(fn [[k v]]
(let [sub (keys-in v)
nested (map #(into [k] %) (filter (comp not empty?) sub))]
(if (seq nested)
nested
[[k]])))
tree)) []))
(defn continuations [cnt words]
(let [index (count words)
path (map #(get % index) words)
trie (get-in words-trie (vec (cons cnt path)))
options (map #(apply str (concat path %)) (keys-in trie))]
(if (= cnt (inc (count words)))
(map #(conj words %) options)
(mapcat #(continuations cnt (conj words %))
(filter
(fn [word]
(not (empty?
(for [idx (range (inc index) cnt)
:let [letter (get word idx)]
:when (get-in words-trie
(vec (concat (list cnt)
(map #(get % idx) words)
(list letter))))]
true))))
options)))))
(continuations 5 ["every" ])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment