Skip to content

Instantly share code, notes, and snippets.

@zehnpaard
Last active May 24, 2016 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zehnpaard/944a5652bede0162b903b691c57c4cb0 to your computer and use it in GitHub Desktop.
Save zehnpaard/944a5652bede0162b903b691c57c4cb0 to your computer and use it in GitHub Desktop.
N-Queen Problem Solver in Clojure
(declare valid-position?)
(defn n-queen [n]
(letfn [(f [positions i]
(filter valid-position?
(for [position positions
piece (map #(vector i %) (range n))]
(conj position piece))))]
(reduce f '(()) (range n))))
(defn valid-position? [position]
(every? #(apply distinct? (map % position))
[first second #(apply + %) #(apply - %)]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment