Commits

John Chandler committed c2eb21b

Minor change to refine-row to handle vectors of possibilities

Comments (0)

Files changed (2)

src/pseudohkoo/core.clj

 (ns pseudohkoo.core)
 (require 'clojure.set)
 
+; NOTE: some of these functions will disappear - I've been writing them to
+;       help me understand Clojure as well as aspects of the problem space
+
+
 (defn extract-slice [vec index]
   ; extracts a three item slice from vec which corresponds to the slice of 3x3 box index is within
   ; this needs a better description!
   ; returns the value of the grid cell at the given location, or nil if out-of-bounds
   (nth (get-row grid row-index) column-index nil))
 
+(defn format-value [value]
+  ; returns either an integer or a vector, depending on whether
+  ; value holds single or multiple values respectively
+  (if (= 1 (count value))
+    (first value)
+    (vec value)))
+
 (defn refine-row [row]
   ; return a row with non-definite values refined, where possible
   (let [required-values (set (range 1 (+ (count row) 1)))
-	current-values  (set row)
+	current-values  (set (filter integer? row))
 	missing-values  (clojure.set/difference required-values current-values)]
     (map (fn [cell]
 	   (if (nil? cell)
-	     (if (= 1 (count missing-values))
-	       (first missing-values)
-	       (vec missing-values))
-	     cell))
+	     (format-value missing-values)
+	     (if (vector? cell)
+	       (format-value (clojure.set/intersection missing-values (set cell)))
+	       cell)))
 	 row)))
 
 (defn refine-grid [grid]

test/pseudohkoo/test/core.clj

     (is (true? (grid-complete? grid)))))
 
 
-
 ;; refine-row
 (deftest test-refine-row-nearly-done
   ; test refining of a "nearly done" row i.e. we're missing a value or two
-  (is (= [1 2 3, 4 5 6, 7 8 9]         (refine-row [1 2 3, 4 5 6, 7 8 nil]) ))
-  (is (= [1 2 [3 9], 4 5 6, 7 8 [3 9]] (refine-row [1 2 nil, 4 5 6, 7 8 nil]) )))
+  (is (= [1 2 3, 4 5 6, 7 8 9]         (refine-row [1 2 3, 4 5 6, 7 8 nil])))
+  (is (= [1 2 [3 9], 4 5 6, 7 8 [3 9]] (refine-row [1 2 nil, 4 5 6, 7 8 nil])))
+  (is (= [1 2 3, 4 5 6, 7 8 9]         (refine-row [1 2 3, 4 5 6, 7 8 [3 9]]))))
 
 
 ;; refine-grid