Skip to content

Instantly share code, notes, and snippets.

@rociiu
Forked from swannodette/gist:3213107
Created July 31, 2012 08:55
Show Gist options
  • Save rociiu/3215182 to your computer and use it in GitHub Desktop.
Save rociiu/3215182 to your computer and use it in GitHub Desktop.
sudoku.clj
(defn distincto [s]
(if (seq s)
(all
(distinctfd (first s))
(distincto (next s)))
s#))
(defn all-infd [xs d]
(if (seq xs)
(all
(domfd (first xs) d)
(all-infd (next xs) d))
s#))
(defn get-square [grid x y]
(for [x (range x (+ x 3))
y (range y (+ y 3))]
(get-in grid [x y])))
(defn init-all [grid inits]
(if (seq inits)
(let [[pos v] (first inits)]
(all
(== (get-in grid pos) v)
(init-all grid (next inits))))
s#))
(defn big-sudokufd [init]
(let [vs (repeatedly 81 lvar)
grid (->> vs
(partition 9) (map vec) (into []))
rows grid
cols (apply map vector grid)
sqs (for [x (range 0 9 3)
y (range 0 9 3)]
(get-square grid x y))]
(run-nc 1 [q]
(== q grid)
(all-infd vs (domain 1 2 3 4 5 6 7 8 9))
(init-all grid init)
(distincto rows)
(distincto cols)
(distincto sqs))))
(comment
(big-sudokufd
[[[0 4] 2] [[0 6] 9] [[1 4] 6] [[1 5] 3]
[[1 8] 8] [[2 0] 3] [[2 5] 8] [[2 6] 1]
[[2 7] 4] [[3 4] 4] [[3 6] 8] [[3 8] 7]
[[4 1] 8] [[4 2] 4] [[4 5] 6] [[4 6] 3]])
;; ~950ms
;; ~95ms to solve 1
(dotimes [_ 5]
(time
(dotimes [_ 10]
(big-sudokufd init0))))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment