Skip to content

Instantly share code, notes, and snippets.

@martintrojer
Created July 7, 2012 11:28
Show Gist options
  • Save martintrojer/3065962 to your computer and use it in GitHub Desktop.
Save martintrojer/3065962 to your computer and use it in GitHub Desktop.
queens blog
(def subo
(fn ([x y r]
(conde
[(== x 0) (== y 0) (== r 0)]
[(== x 0) (== y 1) (== r -1)]
[(== x 1) (== y 0) (== r 1)]
[(== x 1) (== y 1) (== r 0)]))))
(run* [r]
(fresh [x1 x2 x3 x4]
(safeo [x1 0] [x2 1])
(safeo [x1 0] [x3 2])
(safeo [x1 0] [x4 3])
(safeo [x2 1] [x3 2])
(safeo [x2 1] [x4 3])
(safeo [x3 2] [x4 3])
(== r [[x1 0] [x2 1] [x3 2] [x4 3]]))))
(defn safe? [[x1 y1] [x2 y2]]
(and
(not= x1 x2)
(not= y1 y2)
(not= (- x2 x1) (- y2 y1))
(not= (- x1 y2) (- x2 y1))))
(defn safeo
"Are 2 queens threatening each ohter?"
[[x1 y1] [x2 y2]]
(fresh [d1 d2 d3 d4]
(subo x2 x1 d1)
(subo y2 y1 d2)
(subo x1 y2 d3)
(subo x2 y1 d4)
(!= x1 x2)
(!= y1 y2)
(!= d1 d2)
(!= d3 d4)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment