Skip to content

Instantly share code, notes, and snippets.

@martintrojer
Created August 2, 2012 20:43
Show Gist options
  • Save martintrojer/3240455 to your computer and use it in GitHub Desktop.
Save martintrojer/3240455 to your computer and use it in GitHub Desktop.
nqueens-ckanren
(defn diago [qi qj d rng]
(fresh [qid qjd]
(infd qid qjd rng)
(+fd qi d qid)
(!=fd qid qj)
(+fd qj d qjd)
(!=fd qjd qi)))
(defn diagonalso [n r]
((fn dloop [r i s j]
(cond
(or (empty? r) (empty? (rest r))) s#
(empty? s) (dloop (rest r) (inc i) (rest (rest r)) (+ i 2))
:else (let [qi (first r), qj (first s)]
(all
(diago qi qj (- j i) (interval 0 (* 2 n)))
(dloop r i (rest s) (inc j))))))
r 0 (rest r) 1))
(defn n-queenso [q n]
((fn qloop [i l]
(if (zero? i)
(all
(distinctfd l)
(diagonalso n l)
(== q l))
(fresh [x]
(infd x (interval 1 n))
(qloop (dec i) (cons x l)))))
n '()))
(count
(run* [q]
(n-queenso q 8)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment