Skip to content

Instantly share code, notes, and snippets.

@ponzao
Last active December 22, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ponzao/6484911 to your computer and use it in GitHub Desktop.
Save ponzao/6484911 to your computer and use it in GitHub Desktop.
Declarative (= bloated) Tic-Tac-Toe solver in Clojure and core.logic.
(defn solve
[grid]
(remove nil?
(run* [q]
(macro/symbol-macrolet [_ (lvar)]
(== q grid)
(conde
((== grid [[q _ _]
[_ q _]
[_ _ q]]))
((== grid [[_ _ q]
[_ q _]
[q _ _]]))
((== grid [[q _ _]
[q _ _]
[q _ _]]))
((== grid [[_ q _]
[_ q _]
[_ q _]]))
((== grid [[_ _ q]
[_ _ q]
[_ _ q]]))
((== grid [[q q q]
[_ _ _]
[_ _ _]]))
((== grid [[_ _ _]
[q q q]
[_ _ _]]))
((== grid [[_ _ _]
[_ _ _]
[q q q]])))))))
(solve [[:y :x :y]
[nil :x nil]
[nil :x nil]])
; => (:x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment