Permalink
Browse files

convert README to Markdown; update endline characters

  • Loading branch information...
1 parent ffc6985 commit 5a690842f005ad58ba9c83fce0eb102ebbc8d8db @bitsai committed Oct 7, 2011
Showing with 297 additions and 296 deletions.
  1. +0 −6 README
  2. +7 −0 README.md
  3. +9 −9 airplane_puzzle.txt
  4. +83 −83 algorithm.clj
  5. +9 −9 easy_puzzle.txt
  6. +9 −9 hard_puzzle.txt
  7. +9 −9 harder_puzzle.txt
  8. +14 −14 sudoku.clj
  9. +115 −115 sudoku.rb
  10. +42 −42 tests.clj
View
@@ -1,6 +0,0 @@
-Source:
-http://norvig.com/sudoku.html
-
-To solve a puzzle, save it in a file (see the sample puzzle files for format), then run one of:
-clj sudoku.clj <puzzle file>
-ruby sudoku.rb <puzzle file>
View
@@ -0,0 +1,7 @@
+[Inspiration](http://norvig.com/sudoku.html)
+
+To solve a puzzle, save it in a file (see the sample puzzle files for format), then run one of:
+
+ clj sudoku.clj <puzzle file>
+
+ ruby sudoku.rb <puzzle file>
View
@@ -1,9 +1,9 @@
-85..47..3
-...856...
-.........
-4...2..68
-.6.4.3.7.
-39..6...4
-.........
-...175...
-7..23..89
+85..47..3
+...856...
+.........
+4...2..68
+.6.4.3.7.
+39..6...4
+.........
+...175...
+7..23..89
View
@@ -1,83 +1,83 @@
-(ns algorithm
- (:require [clojure.string :as str]))
-
-(defn cross [as bs]
- (for [a as, b bs] (str a b)))
-
-(defn all? [coll]
- (every? identity coll))
-
-(defn copy [values]
- (atom @values))
-
-(def digits (set "123456789"))
-(def rows "ABCDEFGHI")
-(def cols digits)
-(def squares (cross rows cols))
-(def unitlist
- (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))))
-(def units
- (into {} (for [s squares] [s (filter #(some #{s} %) unitlist)])))
-(def peers
- (into {} (for [s squares] [s (disj (set (flatten (units s))) s)])))
-
-(declare assign eliminate helper-1 helper-2)
-
-(defn assign [values s d]
- (let [other-values (disj (@values s) d)]
- (if (every? #(eliminate values s %) other-values)
- values)))
-
-(defn eliminate [values s d]
- (if-not ((@values s) d)
- values
- (let [other-values (disj (@values s) d)]
- (swap! values assoc s other-values)
- (cond
- (not (helper-1 values s)) false
- (not (helper-2 values s d)) false
- :else values))))
-
-;; If a square s is reduced to 1 value d2, then eliminate d2 from peers.
-(defn helper-1 [values s]
- (case (count (@values s))
- 0 false
- 1 (let [d2 (first (@values 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.
-(defn helper-2 [values s d]
- (all? (for [u (units s)]
- (let [dplaces (filter #((@values %) d) u)]
- (case (count dplaces)
- 0 false
- 1 (assign values (first dplaces) d)
- true)))))
-
-(defn grid-values [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)))
- values)))
-
-(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 (copy values) s %)) (@values s)))))
-
-(defn solve [grid]
- (search (parse-grid grid)))
-
-(defn solve-file [file]
- (let [grid (remove #{\newline \return} (slurp file))]
- (solve grid)))
+(ns algorithm
+ (:require [clojure.string :as str]))
+
+(defn cross [as bs]
+ (for [a as, b bs] (str a b)))
+
+(defn all? [coll]
+ (every? identity coll))
+
+(defn copy [values]
+ (atom @values))
+
+(def digits (set "123456789"))
+(def rows "ABCDEFGHI")
+(def cols digits)
+(def squares (cross rows cols))
+(def unitlist
+ (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))))
+(def units
+ (into {} (for [s squares] [s (filter #(some #{s} %) unitlist)])))
+(def peers
+ (into {} (for [s squares] [s (disj (set (flatten (units s))) s)])))
+
+(declare assign eliminate helper-1 helper-2)
+
+(defn assign [values s d]
+ (let [other-values (disj (@values s) d)]
+ (if (every? #(eliminate values s %) other-values)
+ values)))
+
+(defn eliminate [values s d]
+ (if-not ((@values s) d)
+ values
+ (let [other-values (disj (@values s) d)]
+ (swap! values assoc s other-values)
+ (cond
+ (not (helper-1 values s)) false
+ (not (helper-2 values s d)) false
+ :else values))))
+
+;; If a square s is reduced to 1 value d2, then eliminate d2 from peers.
+(defn helper-1 [values s]
+ (case (count (@values s))
+ 0 false
+ 1 (let [d2 (first (@values 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.
+(defn helper-2 [values s d]
+ (all? (for [u (units s)]
+ (let [dplaces (filter #((@values %) d) u)]
+ (case (count dplaces)
+ 0 false
+ 1 (assign values (first dplaces) d)
+ true)))))
+
+(defn grid-values [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)))
+ values)))
+
+(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 (copy values) s %)) (@values s)))))
+
+(defn solve [grid]
+ (search (parse-grid grid)))
+
+(defn solve-file [file]
+ (let [grid (remove #{\newline \return} (slurp file))]
+ (solve grid)))
View
@@ -1,9 +1,9 @@
-..3.2.6..
-9..3.5..1
-..18.64..
-..81.29..
-7.......8
-..67.82..
-..26.95..
-8..2.3..9
-..5.1.3..
+..3.2.6..
+9..3.5..1
+..18.64..
+..81.29..
+7.......8
+..67.82..
+..26.95..
+8..2.3..9
+..5.1.3..
View
@@ -1,9 +1,9 @@
-4.....8.5
-.3.......
-...7.....
-.2.....6.
-....8.4..
-....1....
-...6.3.7.
-5..2.....
-1.4......
+4.....8.5
+.3.......
+...7.....
+.2.....6.
+....8.4..
+....1....
+...6.3.7.
+5..2.....
+1.4......
View
@@ -1,9 +1,9 @@
-.......12
-....35...
-...6...7.
-7.....3..
-...4..8..
-1........
-...12....
-.8.....4.
-.5....6..
+.......12
+....35...
+...6...7.
+7.....3..
+...4..8..
+1........
+...12....
+.8.....4.
+.5....6..
View
@@ -1,14 +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*)))
+(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*)))
Oops, something went wrong.

0 comments on commit 5a69084

Please sign in to comment.