Created
November 21, 2017 16:28
-
-
Save selfsame/59ac4fd3efd33b056923b0d9dc4867c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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