Permalink
Browse files

solving cells with naked pairs

  • Loading branch information...
1 parent 96af092 commit 74e5725fa941a823e760d44566fd4a3c253827bc @IanEsling committed Jun 3, 2012
Showing with 42 additions and 1 deletion.
  1. +8 −1 src/sudoku_clj/solvers/naked_pairs_solver.clj
  2. +34 −0 test/sudoku_clj/test/solvers/naked_pairs.clj
@@ -6,4 +6,11 @@
(defn remove-naked-pairs-in-unit
[unit]
- unit)
+ (loop [pairs (keys (filter #(= 2 (count (val %))) (group-by #(val %) unit)))
+ new-unit unit]
+ (if-let [pair (first pairs)]
+ (recur (next pairs)
+ (reduce
+ #(into %1 {(key %2) (if-not (= (val %2) pair) (difference (val %2) pair) (val %2))}) {} new-unit))
+ new-unit)
+ ))
@@ -12,4 +12,38 @@
(get-cell-numbers 1 7 (remove-naked-pairs-in-unit (get-row-number 1 board))) => #{7 8}
(get-cell-numbers 1 8 (remove-naked-pairs-in-unit (get-row-number 1 board))) => #{7 8}
(get-cell-numbers 1 9 (remove-naked-pairs-in-unit (get-row-number 1 board))) => #{9}
+ )
+
+(fact "naked pairs will have their numbers removed from the numbers for all cells in the same region"
+ (def partially-solved {[1 3] #{1 2} [2 3] #{1 2} [3 3] #{1 2 3}})
+ (def board (update-cells-in-board (create-board [9 6 0 0 0 0 0 0 0
+ 8 5 0 0 0 0 0 0 0
+ 7 4 0 0 0 0 0 0 0
+ 6 0 0 0 0 0 0 0 0
+ 5 0 0 0 0 0 0 0 0
+ 4 0 0 0 0 0 0 0 0
+ 0 0 0 0 0 0 0 0 0
+ 0 0 0 0 0 0 0 0 0
+ 0 0 0 0 0 0 0 0 0
+ ]) partially-solved))
+ (get-cell-numbers 1 3 (remove-naked-pairs-in-unit (get-region-number 1 board))) => #{1 2}
+ (get-cell-numbers 2 3 (remove-naked-pairs-in-unit (get-region-number 1 board))) => #{1 2}
+ (get-cell-numbers 3 3 (remove-naked-pairs-in-unit (get-region-number 1 board))) => #{3}
+ )
+
+(fact "naked pairs will have their numbers removed from the numbers for all cells in the same column"
+ (def partially-solved {[7 1] #{1 2} [8 1] #{1 2} [9 1] #{1 2 3}})
+ (def board (update-cells-in-board (create-board [9 0 0 0 0 0 0 0 0
+ 8 0 0 0 0 0 0 0 0
+ 7 0 0 0 0 0 0 0 0
+ 6 0 0 0 0 0 0 0 0
+ 5 0 0 0 0 0 0 0 0
+ 4 0 0 0 0 0 0 0 0
+ 0 0 0 0 0 0 0 0 0
+ 0 0 0 0 0 0 0 0 0
+ 0 0 0 0 0 0 0 0 0
+ ]) partially-solved))
+ (get-cell-numbers 7 1 (remove-naked-pairs-in-unit (get-column-number 1 board))) => #{1 2}
+ (get-cell-numbers 8 1 (remove-naked-pairs-in-unit (get-column-number 1 board))) => #{1 2}
+ (get-cell-numbers 9 1 (remove-naked-pairs-in-unit (get-column-number 1 board))) => #{3}
)

0 comments on commit 74e5725

Please sign in to comment.