Skip to content

Instantly share code, notes, and snippets.

@springcoil
Forked from swannodette/gist:3217582
Created August 1, 2012 22:41

Revisions

  1. @swannodette swannodette revised this gist Aug 1, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@
    y (range 0 9 3)]
    (get-square rows x y))]
    (run 1 [q]
    (== q rows)
    (== q vars)
    (everyo #(infd % (domain 1 2 3 4 5 6 7 8 9)) vars)
    (init vars hints)
    (everyo distinctfd rows)
  2. @swannodette swannodette revised this gist Aug 1, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@
    sqs (for [x (range 0 9 3)
    y (range 0 9 3)]
    (get-square rows x y))]
    (run-nc 1 [q]
    (run 1 [q]
    (== q rows)
    (everyo #(infd % (domain 1 2 3 4 5 6 7 8 9)) vars)
    (init vars hints)
  3. @swannodette swannodette revised this gist Aug 1, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -11,10 +11,10 @@
    (if (seq vars)
    (let [hint (first hints)]
    (all
    (if-not (zero? hint)
    (== (first vars) hint)
    succeed)
    (init (next vars) (next hints))))
    (if-not (zero? hint)
    (== (first vars) hint)
    succeed)
    (init (next vars) (next hints))))
    succeed))

    (defn sudokufd [hints]
  4. @swannodette swannodette revised this gist Aug 1, 2012. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -17,8 +17,6 @@
    (init (next vars) (next hints))))
    succeed))

    ;; hints is a vector of integers, only values
    ;; that are not zero will be used
    (defn sudokufd [hints]
    (let [vars (repeatedly 81 lvar)
    rows (->> vars (partition 9) (map vec) (into []))
  5. @swannodette swannodette revised this gist Aug 1, 2012. 1 changed file with 18 additions and 1 deletion.
    19 changes: 18 additions & 1 deletion gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -32,4 +32,21 @@
    (init vars hints)
    (everyo distinctfd rows)
    (everyo distinctfd cols)
    (everyo distinctfd sqs))))
    (everyo 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])
    )
  6. @swannodette swannodette revised this gist Aug 1, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@
    (init (next vars) (next hints))))
    succeed))

    ;; hints is a vector of integer, only values
    ;; hints is a vector of integers, only values
    ;; that are not zero will be used
    (defn sudokufd [hints]
    (let [vars (repeatedly 81 lvar)
  7. @swannodette swannodette revised this gist Aug 1, 2012. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,8 @@
    (init (next vars) (next hints))))
    succeed))

    ;; hints is vectors of ints, only values that are not zero will be used
    ;; hints is a vector of integer, only values
    ;; that are not zero will be used
    (defn sudokufd [hints]
    (let [vars (repeatedly 81 lvar)
    rows (->> vars (partition 9) (map vec) (into []))
  8. @swannodette swannodette revised this gist Aug 1, 2012. 1 changed file with 19 additions and 12 deletions.
    31 changes: 19 additions & 12 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -2,26 +2,33 @@
    (:refer-clojure :exclude [==])
    (:use clojure.core.logic))

    (defn get-square [grid x y]
    (defn get-square [rows x y]
    (for [x (range x (+ x 3))
    y (range y (+ y 3))]
    (get-in grid [x y])))
    (get-in rows [x y])))

    (defn init [grid [pos value]]
    (== (get-in grid pos) value))
    (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))

    ;; hints is vectors of ints, only values that are not zero will be used
    (defn sudokufd [hints]
    (let [vs (repeatedly 81 lvar)
    grid (->> vs (partition 9) (map vec) (into []))
    rows grid
    cols (apply map vector grid)
    (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 grid x y))]
    (get-square rows x y))]
    (run-nc 1 [q]
    (== q grid)
    (everyo #(infd % (domain 1 2 3 4 5 6 7 8 9)) vs)
    (everyo (partial init grid) hints)
    (== q rows)
    (everyo #(infd % (domain 1 2 3 4 5 6 7 8 9)) vars)
    (init vars hints)
    (everyo distinctfd rows)
    (everyo distinctfd cols)
    (everyo distinctfd sqs))))
  9. @swannodette swannodette revised this gist Jul 31, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    (defn init [grid [pos value]]
    (== (get-in grid pos) value))

    (defn sudokufd [initial]
    (defn sudokufd [hints]
    (let [vs (repeatedly 81 lvar)
    grid (->> vs (partition 9) (map vec) (into []))
    rows grid
    @@ -21,7 +21,7 @@
    (run-nc 1 [q]
    (== q grid)
    (everyo #(infd % (domain 1 2 3 4 5 6 7 8 9)) vs)
    (everyo (partial init grid) initial)
    (everyo (partial init grid) hints)
    (everyo distinctfd rows)
    (everyo distinctfd cols)
    (everyo distinctfd sqs))))
  10. @swannodette swannodette revised this gist Jul 31, 2012. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    (ns sudoku
    (:refer-clojure :exclude [==])
    (:use clojure.core.logic))

    (defn get-square [grid x y]
    (for [x (range x (+ x 3))
    y (range y (+ y 3))]
    @@ -20,4 +24,4 @@
    (everyo (partial init grid) initial)
    (everyo distinctfd rows)
    (everyo distinctfd cols)
    (everyo distinctfd sqs))))
    (everyo distinctfd sqs))))
  11. @swannodette swannodette revised this gist Jul 31, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    (defn init [grid [pos value]]
    (== (get-in grid pos) value))

    (defn big-sudokufd [initial]
    (defn sudokufd [initial]
    (let [vs (repeatedly 81 lvar)
    grid (->> vs (partition 9) (map vec) (into []))
    rows grid
  12. @swannodette swannodette created this gist Jul 31, 2012.
    23 changes: 23 additions & 0 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    (defn get-square [grid x y]
    (for [x (range x (+ x 3))
    y (range y (+ y 3))]
    (get-in grid [x y])))

    (defn init [grid [pos value]]
    (== (get-in grid pos) value))

    (defn big-sudokufd [initial]
    (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)
    (everyo #(infd % (domain 1 2 3 4 5 6 7 8 9)) vs)
    (everyo (partial init grid) initial)
    (everyo distinctfd rows)
    (everyo distinctfd cols)
    (everyo distinctfd sqs))))