Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

sync Clojure sudoku code

  • Loading branch information...
commit ffc69855ace8c95f9f002426c6b78984a364957b 1 parent 0953427
@bitsai authored
Showing with 24 additions and 21 deletions.
  1. +10 −17 algorithm.clj
  2. +10 −0 sudoku.clj
  3. +4 −4 tests.clj
View
27 algorithm.clj
@@ -1,16 +1,16 @@
(ns algorithm
(:require [clojure.string :as str]))
-(defn cross [A B]
- (for [a A b B] (str a b)))
+(defn cross [as bs]
+ (for [a as, b bs] (str a b)))
(defn all? [coll]
(every? identity coll))
-(defn interpose-nth [n sep coll]
- (apply concat (interpose [sep] (partition n coll))))
+(defn copy [values]
+ (atom @values))
-(def digits (set (range 1 10)))
+(def digits (set "123456789"))
(def rows "ABCDEFGHI")
(def cols digits)
(def squares (cross rows cols))
@@ -18,7 +18,7 @@
(concat
(for [c cols] (cross rows [c]))
(for [r rows] (cross [r] cols))
- (for [rs (partition 3 rows) cs (partition 3 cols)] (cross rs cs))))
+ (for [rs (partition 3 rows), cs (partition 3 cols)] (cross rs cs))))
(def units
(into {} (for [s squares] [s (filter #(some #{s} %) unitlist)])))
(def peers
@@ -46,7 +46,7 @@
(case (count (@values s))
0 false
1 (let [d2 (first (@values s))]
- (every? #(eliminate values % d2) (peers s)))
+ (every? #(eliminate values % d2) (peers s)))
true))
;; If a unit u is reduced to 1 place for a value d, then put it there.
@@ -59,28 +59,21 @@
true)))))
(defn grid-values [grid]
- (zipmap squares (map #(Character/digit % 10) grid)))
+ (zipmap squares grid))
(defn parse-grid [grid]
(let [values (atom (zipmap squares (repeat digits)))]
(if (all? (for [[s d] (grid-values grid) :when (digits d)]
- (assign values s d)))
+ (assign values s d)))
values)))
-(defn display [values]
- (let [rows (partition 9 (map #(apply str (@values %)) squares))
- lines (map #(str/join " " (interpose-nth 3 "|" %)) rows)
- separator-line (apply str (repeat 21 "-"))]
- (doseq [line (interpose-nth 3 separator-line lines)]
- (println line))))
-
(defn search [values]
(cond
(not values) false
(every? #(= 1 (count (@values %))) squares) values
:else (let [unfilled (filter #(> (count (@values %)) 1) squares)
s (apply min-key #(count (@values %)) unfilled)]
- (some #(search (assign (atom @values) s %)) (@values s)))))
+ (some #(search (assign (copy values) s %)) (@values s)))))
(defn solve [grid]
(search (parse-grid grid)))
View
10 sudoku.clj
@@ -1,4 +1,14 @@
(ns sudoku
(:use algorithm))
+(defn interpose-nth [n sep coll]
+ (apply concat (interpose [sep] (partition n coll))))
+
+(defn display [values]
+ (let [rows (partition 9 (map #(apply str (@values %)) squares))
+ lines (map #(str/join " " (interpose-nth 3 "|" %)) rows)
+ separator-line (apply str (repeat 21 "-"))]
+ (doseq [line (interpose-nth 3 separator-line lines)]
+ (println line))))
+
(display (solve-file (first *command-line-args*)))
View
8 tests.clj
@@ -28,15 +28,15 @@
"A1" "A3" "B1" "B3"}
@(solve-file "airplane_puzzle.txt")
- {"I1" #{7}, "H1" #{9}, "I2" #{1}, "G1" #{5}, "H2" #{8}, "I3" #{6}, "F1" #{3}, "G2" #{2}, "H3" #{3}, "I4" #{2}, "E1" #{2}, "F2" #{9}, "G3" #{4}, "H4" #{1}, "I5" #{3}, "D1" #{4}, "E2" #{6}, "F3" #{8}, "G4" #{6}, "H5" #{7}, "I6" #{4}, "C1" #{6}, "D2" #{7}, "E3" #{5}, "F4" #{7}, "G5" #{9}, "H6" #{5}, "I7" #{5}, "B1" #{1}, "C2" #{4}, "D3" #{1}, "E4" #{4}, "F5" #{6}, "G6" #{8}, "H7" #{4}, "I8" #{8}, "A1" #{8}, "B2" #{3}, "C3" #{7}, "D4" #{5}, "E5" #{8}, "F6" #{1}, "G7" #{1}, "H8" #{2}, "I9" #{9}, "A2" #{5}, "B3" #{9}, "C4" #{3}, "D5" #{2}, "E6" #{3}, "F7" #{2}, "G8" #{3}, "H9" #{6}, "A3" #{2}, "B4" #{8}, "C5" #{1}, "D6" #{9}, "E7" #{9}, "F8" #{5}, "G9" #{7}, "A4" #{9}, "B5" #{5}, "C6" #{2}, "D7" #{3}, "E8" #{7}, "F9" #{4}, "A5" #{4}, "B6" #{6}, "C7" #{8}, "D8" #{6}, "E9" #{1}, "A6" #{7}, "B7" #{7}, "C8" #{9}, "D9" #{8}, "A7" #{6}, "B8" #{4}, "C9" #{5}, "A8" #{1}, "B9" #{2}, "A9" #{3}}
+ {"I1" #{\7}, "H1" #{\9}, "I2" #{\1}, "G1" #{\5}, "H2" #{\8}, "I3" #{\6}, "F1" #{\3}, "G2" #{\2}, "H3" #{\3}, "I4" #{\2}, "E1" #{\2}, "F2" #{\9}, "G3" #{\4}, "H4" #{\1}, "I5" #{\3}, "D1" #{\4}, "E2" #{\6}, "F3" #{\8}, "G4" #{\6}, "H5" #{\7}, "I6" #{\4}, "C1" #{\6}, "D2" #{\7}, "E3" #{\5}, "F4" #{\7}, "G5" #{\9}, "H6" #{\5}, "I7" #{\5}, "B1" #{\1}, "C2" #{\4}, "D3" #{\1}, "E4" #{\4}, "F5" #{\6}, "G6" #{\8}, "H7" #{\4}, "I8" #{\8}, "A1" #{\8}, "B2" #{\3}, "C3" #{\7}, "D4" #{\5}, "E5" #{\8}, "F6" #{\1}, "G7" #{\1}, "H8" #{\2}, "I9" #{\9}, "A2" #{\5}, "B3" #{\9}, "C4" #{\3}, "D5" #{\2}, "E6" #{\3}, "F7" #{\2}, "G8" #{\3}, "H9" #{\6}, "A3" #{\2}, "B4" #{\8}, "C5" #{\1}, "D6" #{\9}, "E7" #{\9}, "F8" #{\5}, "G9" #{\7}, "A4" #{\9}, "B5" #{\5}, "C6" #{\2}, "D7" #{\3}, "E8" #{\7}, "F9" #{\4}, "A5" #{\4}, "B6" #{\6}, "C7" #{\8}, "D8" #{\6}, "E9" #{\1}, "A6" #{\7}, "B7" #{\7}, "C8" #{\9}, "D9" #{\8}, "A7" #{\6}, "B8" #{\4}, "C9" #{\5}, "A8" #{\1}, "B9" #{\2}, "A9" #{\3}}
@(solve-file "easy_puzzle.txt")
- {"I1" #{6}, "H1" #{8}, "I2" #{9}, "G1" #{3}, "H2" #{1}, "I3" #{5}, "F1" #{1}, "G2" #{7}, "H3" #{4}, "I4" #{4}, "E1" #{7}, "F2" #{3}, "G3" #{2}, "H4" #{2}, "I5" #{1}, "D1" #{5}, "E2" #{2}, "F3" #{6}, "G4" #{6}, "H5" #{5}, "I6" #{7}, "C1" #{2}, "D2" #{4}, "E3" #{9}, "F4" #{7}, "G5" #{8}, "H6" #{3}, "I7" #{3}, "B1" #{9}, "C2" #{5}, "D3" #{8}, "E4" #{5}, "F5" #{9}, "G6" #{9}, "H7" #{7}, "I8" #{8}, "A1" #{4}, "B2" #{6}, "C3" #{1}, "D4" #{1}, "E5" #{6}, "F6" #{8}, "G7" #{5}, "H8" #{6}, "I9" #{2}, "A2" #{8}, "B3" #{7}, "C4" #{8}, "D5" #{3}, "E6" #{4}, "F7" #{2}, "G8" #{1}, "H9" #{9}, "A3" #{3}, "B4" #{3}, "C5" #{7}, "D6" #{2}, "E7" #{1}, "F8" #{4}, "G9" #{4}, "A4" #{9}, "B5" #{4}, "C6" #{6}, "D7" #{9}, "E8" #{3}, "F9" #{5}, "A5" #{2}, "B6" #{5}, "C7" #{4}, "D8" #{7}, "E9" #{8}, "A6" #{1}, "B7" #{8}, "C8" #{9}, "D9" #{6}, "A7" #{6}, "B8" #{2}, "C9" #{3}, "A8" #{5}, "B9" #{1}, "A9" #{7}}
+ {"I1" #{\6}, "H1" #{\8}, "I2" #{\9}, "G1" #{\3}, "H2" #{\1}, "I3" #{\5}, "F1" #{\1}, "G2" #{\7}, "H3" #{\4}, "I4" #{\4}, "E1" #{\7}, "F2" #{\3}, "G3" #{\2}, "H4" #{\2}, "I5" #{\1}, "D1" #{\5}, "E2" #{\2}, "F3" #{\6}, "G4" #{\6}, "H5" #{\5}, "I6" #{\7}, "C1" #{\2}, "D2" #{\4}, "E3" #{\9}, "F4" #{\7}, "G5" #{\8}, "H6" #{\3}, "I7" #{\3}, "B1" #{\9}, "C2" #{\5}, "D3" #{\8}, "E4" #{\5}, "F5" #{\9}, "G6" #{\9}, "H7" #{\7}, "I8" #{\8}, "A1" #{\4}, "B2" #{\6}, "C3" #{\1}, "D4" #{\1}, "E5" #{\6}, "F6" #{\8}, "G7" #{\5}, "H8" #{\6}, "I9" #{\2}, "A2" #{\8}, "B3" #{\7}, "C4" #{\8}, "D5" #{\3}, "E6" #{\4}, "F7" #{\2}, "G8" #{\1}, "H9" #{\9}, "A3" #{\3}, "B4" #{\3}, "C5" #{\7}, "D6" #{\2}, "E7" #{\1}, "F8" #{\4}, "G9" #{\4}, "A4" #{\9}, "B5" #{\4}, "C6" #{\6}, "D7" #{\9}, "E8" #{\3}, "F9" #{\5}, "A5" #{\2}, "B6" #{\5}, "C7" #{\4}, "D8" #{\7}, "E9" #{\8}, "A6" #{\1}, "B7" #{\8}, "C8" #{\9}, "D9" #{\6}, "A7" #{\6}, "B8" #{\2}, "C9" #{\3}, "A8" #{\5}, "B9" #{\1}, "A9" #{\7}}
@(solve-file "hard_puzzle.txt")
- {"I1" #{1}, "H1" #{5}, "I2" #{6}, "G1" #{2}, "H2" #{7}, "I3" #{4}, "F1" #{3}, "G2" #{8}, "H3" #{3}, "I4" #{8}, "E1" #{7}, "F2" #{4}, "G3" #{9}, "H4" #{2}, "I5" #{7}, "D1" #{8}, "E2" #{9}, "F3" #{6}, "G4" #{6}, "H5" #{9}, "I6" #{5}, "C1" #{9}, "D2" #{2}, "E3" #{1}, "F4" #{9}, "G5" #{4}, "H6" #{1}, "I7" #{2}, "B1" #{6}, "C2" #{5}, "D3" #{5}, "E4" #{5}, "F5" #{1}, "G6" #{3}, "H7" #{6}, "I8" #{9}, "A1" #{4}, "B2" #{3}, "C3" #{8}, "D4" #{4}, "E5" #{8}, "F6" #{2}, "G7" #{5}, "H8" #{8}, "I9" #{3}, "A2" #{1}, "B3" #{2}, "C4" #{7}, "D5" #{3}, "E6" #{6}, "F7" #{7}, "G8" #{7}, "H9" #{4}, "A3" #{7}, "B4" #{1}, "C5" #{2}, "D6" #{7}, "E7" #{4}, "F8" #{5}, "G9" #{1}, "A4" #{3}, "B5" #{5}, "C6" #{4}, "D7" #{1}, "E8" #{3}, "F9" #{8}, "A5" #{6}, "B6" #{8}, "C7" #{3}, "D8" #{6}, "E9" #{2}, "A6" #{9}, "B7" #{9}, "C8" #{1}, "D9" #{9}, "A7" #{8}, "B8" #{4}, "C9" #{6}, "A8" #{2}, "B9" #{7}, "A9" #{5}}
+ {"I1" #{\1}, "H1" #{\5}, "I2" #{\6}, "G1" #{\2}, "H2" #{\7}, "I3" #{\4}, "F1" #{\3}, "G2" #{\8}, "H3" #{\3}, "I4" #{\8}, "E1" #{\7}, "F2" #{\4}, "G3" #{\9}, "H4" #{\2}, "I5" #{\7}, "D1" #{\8}, "E2" #{\9}, "F3" #{\6}, "G4" #{\6}, "H5" #{\9}, "I6" #{\5}, "C1" #{\9}, "D2" #{\2}, "E3" #{\1}, "F4" #{\9}, "G5" #{\4}, "H6" #{\1}, "I7" #{\2}, "B1" #{\6}, "C2" #{\5}, "D3" #{\5}, "E4" #{\5}, "F5" #{\1}, "G6" #{\3}, "H7" #{\6}, "I8" #{\9}, "A1" #{\4}, "B2" #{\3}, "C3" #{\8}, "D4" #{\4}, "E5" #{\8}, "F6" #{\2}, "G7" #{\5}, "H8" #{\8}, "I9" #{\3}, "A2" #{\1}, "B3" #{\2}, "C4" #{\7}, "D5" #{\3}, "E6" #{\6}, "F7" #{\7}, "G8" #{\7}, "H9" #{\4}, "A3" #{\7}, "B4" #{\1}, "C5" #{\2}, "D6" #{\7}, "E7" #{\4}, "F8" #{\5}, "G9" #{\1}, "A4" #{\3}, "B5" #{\5}, "C6" #{\4}, "D7" #{\1}, "E8" #{\3}, "F9" #{\8}, "A5" #{\6}, "B6" #{\8}, "C7" #{\3}, "D8" #{\6}, "E9" #{\2}, "A6" #{\9}, "B7" #{\9}, "C8" #{\1}, "D9" #{\9}, "A7" #{\8}, "B8" #{\4}, "C9" #{\6}, "A8" #{\2}, "B9" #{\7}, "A9" #{\5}}
@(solve-file "harder_puzzle.txt")
- {"I1" #{3}, "H1" #{2}, "I2" #{5}, "G1" #{4}, "H2" #{8}, "I3" #{1}, "F1" #{1}, "G2" #{6}, "H3" #{7}, "I4" #{9}, "E1" #{5}, "F2" #{3}, "G3" #{9}, "H4" #{3}, "I5" #{4}, "D1" #{7}, "E2" #{2}, "F3" #{4}, "G4" #{1}, "H5" #{5}, "I6" #{7}, "C1" #{8}, "D2" #{9}, "E3" #{6}, "F4" #{5}, "G5" #{2}, "H6" #{6}, "I7" #{6}, "B1" #{9}, "C2" #{4}, "D3" #{8}, "E4" #{4}, "F5" #{8}, "G6" #{8}, "H7" #{1}, "I8" #{2}, "A1" #{6}, "B2" #{1}, "C3" #{5}, "D4" #{2}, "E5" #{7}, "F6" #{9}, "G7" #{7}, "H8" #{4}, "I9" #{8}, "A2" #{7}, "B3" #{2}, "C4" #{6}, "D5" #{6}, "E6" #{3}, "F7" #{2}, "G8" #{3}, "H9" #{9}, "A3" #{3}, "B4" #{7}, "C5" #{1}, "D6" #{1}, "E7" #{8}, "F8" #{6}, "G9" #{5}, "A4" #{8}, "B5" #{3}, "C6" #{2}, "D7" #{3}, "E8" #{9}, "F9" #{7}, "A5" #{9}, "B6" #{5}, "C7" #{9}, "D8" #{5}, "E9" #{1}, "A6" #{4}, "B7" #{4}, "C8" #{7}, "D9" #{4}, "A7" #{5}, "B8" #{8}, "C9" #{3}, "A8" #{1}, "B9" #{6}, "A9" #{2}}))
+ {"I1" #{\3}, "H1" #{\2}, "I2" #{\5}, "G1" #{\4}, "H2" #{\8}, "I3" #{\1}, "F1" #{\1}, "G2" #{\6}, "H3" #{\7}, "I4" #{\9}, "E1" #{\5}, "F2" #{\3}, "G3" #{\9}, "H4" #{\3}, "I5" #{\4}, "D1" #{\7}, "E2" #{\2}, "F3" #{\4}, "G4" #{\1}, "H5" #{\5}, "I6" #{\7}, "C1" #{\8}, "D2" #{\9}, "E3" #{\6}, "F4" #{\5}, "G5" #{\2}, "H6" #{\6}, "I7" #{\6}, "B1" #{\9}, "C2" #{\4}, "D3" #{\8}, "E4" #{\4}, "F5" #{\8}, "G6" #{\8}, "H7" #{\1}, "I8" #{\2}, "A1" #{\6}, "B2" #{\1}, "C3" #{\5}, "D4" #{\2}, "E5" #{\7}, "F6" #{\9}, "G7" #{\7}, "H8" #{\4}, "I9" #{\8}, "A2" #{\7}, "B3" #{\2}, "C4" #{\6}, "D5" #{\6}, "E6" #{\3}, "F7" #{\2}, "G8" #{\3}, "H9" #{\9}, "A3" #{\3}, "B4" #{\7}, "C5" #{\1}, "D6" #{\1}, "E7" #{\8}, "F8" #{\6}, "G9" #{\5}, "A4" #{\8}, "B5" #{\3}, "C6" #{\2}, "D7" #{\3}, "E8" #{\9}, "F9" #{\7}, "A5" #{\9}, "B6" #{\5}, "C7" #{\9}, "D8" #{\5}, "E9" #{\1}, "A6" #{\4}, "B7" #{\4}, "C8" #{\7}, "D9" #{\4}, "A7" #{\5}, "B8" #{\8}, "C9" #{\3}, "A8" #{\1}, "B9" #{\6}, "A9" #{\2}}))
(run-tests)
Please sign in to comment.
Something went wrong with that request. Please try again.