Created
July 31, 2012 14:52
-
-
Save swannodette/3217582 to your computer and use it in GitHub Desktop.
sudoku_compact.clj
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
;; based on core.logic 0.8-alpha2 or core.logic master branch | |
(ns sudoku | |
(:refer-clojure :exclude [==]) | |
(:use clojure.core.logic)) | |
(defn get-square [rows x y] | |
(for [x (range x (+ x 3)) | |
y (range y (+ y 3))] | |
(get-in rows [x y]))) | |
(defn init [vars hints] | |
(if (seq vars) | |
(let [hint (first hints)] | |
(all | |
(if-not (zero? hint) | |
(== (first vars) hint) | |
succeed) | |
(init (next vars) (next hints)))) | |
succeed)) | |
(defn sudokufd [hints] | |
(let [vars (repeatedly 81 lvar) | |
rows (->> vars (partition 9) (map vec) (into [])) | |
cols (apply map vector rows) | |
sqs (for [x (range 0 9 3) | |
y (range 0 9 3)] | |
(get-square rows x y))] | |
(run 1 [q] | |
(== q vars) | |
(everyg #(infd % (domain 1 2 3 4 5 6 7 8 9)) vars) | |
(init vars hints) | |
(everyg distinctfd rows) | |
(everyg distinctfd cols) | |
(everyg distinctfd sqs)))) | |
;; ==== | |
(comment | |
(sudokufd | |
[0 0 3 0 2 0 6 0 0 | |
9 0 0 3 0 5 0 0 1 | |
0 0 1 8 0 6 4 0 0 | |
0 0 8 1 0 2 9 0 0 | |
7 0 0 0 0 0 0 0 8 | |
0 0 6 7 0 8 2 0 0 | |
0 0 2 6 0 9 5 0 0 | |
8 0 0 2 0 3 0 0 9 | |
0 0 5 0 1 0 3 0 0]) | |
) |
Hey David, where did "infd" go? It's nowhere to be found in the master branch. thx!
@postspectacular - it's in clojure.core.logic.fd now. Here's an updated version that I've been using in my core.logic talks:
Dead link: http://dosync.posterous.com/friendlier-shorter. Posterous Spaces are no longer available. Has this been republished elsewhere?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It makes good sense.. many thanks! I guess I'll have no other choice than to go through The Reasoned Schemer, to go deeper.