Skip to content

Instantly share code, notes, and snippets.

@larstvei
Created April 15, 2016 23:22
Show Gist options
  • Save larstvei/305a8ad1efea9d8fd961ea6a7262b726 to your computer and use it in GitHub Desktop.
Save larstvei/305a8ad1efea9d8fd961ea6a7262b726 to your computer and use it in GitHub Desktop.
(defn edge? [w1 w2]
(and (not= w1 w2) (= (last w1) (first w2))))
(defn add-edges [m w words]
(let [edges (filter (partial edge? w) words)]
(assoc m w edges)))
(defn build-graph [words]
(reduce (fn [m w] (add-edges m w words)) {} words))
(defn play [m w]
(if (empty? (m w))
false
(not-every? (partial play (dissoc m w)) (filter m (m w)))))
(defn solve
([states] (filter (partial solve (build-graph states)) states))
([m w] (every? (partial play (dissoc m w)) (m w))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment