Commits

John Chandler committed c0d5aff

Okay, now we have some building blocks in place - starting to build things up

  • Participants
  • Parent commits 42cb36b

Comments (0)

Files changed (3)

 implement the loose set of techniques I use to solve these puzzles. From what
 I have gathered, they are close to the typical methods used by others.
 
+Two rules for the work were 1) only use the standard Clojure API and 2) no
+mutable state i.e. I have to think functionally. The first is easy, the
+second is hard because I don't tend to think in terms of immutable state.
+
 ## Usage
 
 Figure it out yourself. I had to ;-)

src/pseudohkoo/core.clj

 (ns pseudohkoo.core)
+(require 'clojure.set)
 
 (defn extract-slice [vec index]
   ; extracts a three item slice from vec which corresponds to the slice of 3x3 box index is within
   ; 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 refine-row [row]
+  ; return a row with non-definite values rfined, where possible
+  (let [required-values (set (range 1 (+ (count row) 1)))
+	current-values  (set 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))
+	 row)))
+
+(defn refine-grid [grid]
+  ; return a "refined" grid - replacing nil cells with definite or possible values as appropriate 
+  (map refine-row grid))
+
 (defn row-complete? [row]
   ; returns true if row has been completed or false if not
   (let [required-values (set (range 1 (+ (count row) 1)))]

test/pseudohkoo/test/core.clj

     (is (true? (row-complete? row)))))
 
 
-; grid-complete?
+;; grid-complete?
 (deftest test-grid-incomplete
   ; test if grid is incomplete
   (let [grid [[  1   2   3]
 	      [2 3 1]]
        ]
     (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
+  (is (= [1 2 3, 4 5 6, 7 8 9] (refine-row [1 2 3, 4 5 6, 7 8 nil]) )))
+
+
+;; refine-grid
+(deftest test-refine-grid-nearly-done
+  ; test refinement of a "nearly done" grid i.e. we're missing 9s
+  (let [grid  [[  1   2   3,   4   5   6,   7   8 nil]
+	       [nil   1   2,   3   4   5,   6   7   8]
+	       [  8 nil   1,   2   3   4,   5   6   7]
+	       [  7   8 nil,   1   2   3,   4   5   6]
+	       [  6   7   8, nil   1   2,   3   4   5]
+	       [  5   6   7,   8 nil   1,   2   3   4]
+	       [  4   5   6,   7   8 nil,   1   2   3]
+	       [  3   4   5,   6   7   8, nil   1   2]
+	       [  2   3   4,   5   6   7,   8  nil  1]]
+	result (refine-grid grid)
+      ]
+    (is (= [1 2 3, 4 5 6, 7 8 9] (nth result 0)))
+
+    )
+  )