Permalink
Browse files

* src/main/clojure/clojure/core/logic/bench.clj: simplistic verificat…

…ion of sudoku results
  • Loading branch information...
1 parent 5db26ee commit ed614bf2e4b89f2f2eccb171a93dc6b47ebd25fe David Nolen committed Aug 1, 2012
Showing with 30 additions and 5 deletions.
  1. +30 −5 src/main/clojure/clojure/core/logic/bench.clj
View
35 src/main/clojure/clojure/core/logic/bench.clj
@@ -485,13 +485,22 @@
(init (next vars) (next hints))))
succeed))
+(defn ->rows [xs]
+ (->> xs (partition 9) (map vec) (into [])))
+
+(defn ->cols [rows]
+ (apply map vector rows))
+
+(defn ->squares [rows]
+ (for [x (range 0 9 3)
+ y (range 0 9 3)]
+ (get-square rows x y)))
+
(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))]
+ rows (->rows vars)
+ cols (->cols rows)
+ sqs (->squares rows)]
(run-nc 1 [q]
(== q rows)
(everyo #(infd % (domain 1 2 3 4 5 6 7 8 9)) vars)
@@ -500,6 +509,16 @@
(everyo distinctfd cols)
(everyo distinctfd sqs))))
+(defn verify [rows]
+ (let [cols (->cols rows)
+ sqs (->squares rows)
+ verify-group (fn [group]
+ (every? #(= (count (into #{} %)) 9)
+ group))]
+ (and (verify-group rows)
+ (verify-group cols)
+ (verify-group sqs))))
+
(comment
(def easy0
[0 0 3 0 2 0 6 0 0
@@ -516,6 +535,8 @@
(sudokufd easy0)
+ (-> (sudokufd easy0) first verify)
+
;; ~600ms
;; 6ms for 1
(dotimes [_ 5]
@@ -539,6 +560,8 @@
(time (sudokufd hard0))
+ (-> (sudokufd hard0) first verify)
+
;; ~600ms
;; ~60ms for 1
(dotimes [_ 5]
@@ -562,6 +585,8 @@
(time (sudokufd hard1))
+ (-> (sudokufd hard1) first verify)
+
;; ~1500ms
;; ~150ms for 1
(dotimes [_ 5]

0 comments on commit ed614bf

Please sign in to comment.