Last active
April 4, 2016 14:16
-
-
Save jmhdez/f1f0fa796af09f92bf6973d8b46fa18c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; depends on [loco "0.3.1"] | |
(ns loco-playground.core | |
(:require [loco.core :refer :all] | |
[loco.constraints :refer :all])) | |
(def queens-model | |
(conj | |
;; Variables [:x i] [:y i] para la posición de la reina i-ésima | |
(mapcat #(vector ($in [:x %] 0 7) ($in [:y %] 0 7)) (range 8)) | |
;; Todas las filas deben ser distintas | |
($distinct (map (partial vector :x) (range 8))) | |
;; Todas las columnas deben ser distintas | |
($distinct (map (partial vector :y) (range 8))) | |
;; Todas las diagonales deben ser distintas | |
($distinct (map #($- [:x %] [:y %]) (range 8))) | |
;; Toas las contra-diagonales deben ser distintas | |
($distinct (map #($+ [:x %] [:y %]) (range 8))))) | |
(defn place-queens [queens] | |
(let [board (vec (map (fn [_] (vec (take 8 (repeat 0)))) (range 8)))] | |
(->> (range 8) | |
(reduce (fn [b i] (let [x (queens [:x i]) y (queens [:y i])] | |
(assoc-in b [x y] 1))) | |
board)))) | |
;; Busco una solución y la pinto | |
(clojure.pprint/pprint (place-queens (solution queens-model))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment