Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

submission #448

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: clojure
lein: lein2
script: lein2 midje :config .midje-grading-config.clj
lein: lein
script: lein midje :config .midje-grading-config.clj
jdk:
- openjdk7
notifications:
Expand Down
81 changes: 57 additions & 24 deletions src/sudoku.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,89 @@

(def board identity)

(def all-values #{1 2 3 4 5 6 7 8 9})

(defn value-at [board coord]
nil)
(get-in board coord))

(defn has-value? [board coord]
nil)
(not (= 0 (value-at board coord))))

(defn row-values [board coord]
nil)
(defn row-values [board [row _]]
(reduce (fn [vals col] (conj vals (value-at board [row, col]))) #{} (range 9)))

(defn col-values [board coord]
nil)
(defn col-values [board [_ col]]
(reduce (fn [vals row] (conj vals (value-at board [row, col]))) #{} (range 9)))

(defn coord-pairs [coords]
nil)
(for [row coords
col coords]
[row col]))

(defn block-values [board coord]
nil)
(defn block-values [board [row col]]
(let [min-block (fn [x] (* (int (/ x 3)) 3))
coords (for [[r c] (coord-pairs [0 1 2])]
[(+ r (min-block row)) (+ c (min-block col))])]
(reduce (fn [vals [r c]] (conj vals (value-at board [r, c]))) #{} coords)))

(defn valid-values-for [board coord]
nil)
(if (has-value? board coord)
#{}
(let [vals-in-use (set/union (row-values board coord) (col-values board coord) (block-values board coord))]
(set/difference all-values vals-in-use))))

(defn filled? [board]
nil)
(let [vals (set (apply concat board))]
(not (contains? vals 0))))

(defn rows [board]
nil)
(for [row (range 9)]
(row-values board [row 0])))

(defn valid-groups? [seq-of-sets]
(let [is-invalid? (fn [vals] (or (contains? vals 0) (< (count vals) 9)))]
(empty? (filter is-invalid? seq-of-sets))))

(defn valid-rows? [board]
nil)
(defn valid-rows? [board] (valid-groups? (rows board)))

(defn cols [board]
nil)
(for [col (range 9)]
(col-values board [0 col])))

(defn valid-cols? [board]
nil)
(defn valid-cols? [board] (valid-groups? (cols board)))

(defn blocks [board]
nil)
(for [row [0 3 6]
col [0 3 6]]
(block-values board [row col])))

(defn valid-blocks? [board]
nil)
(defn valid-blocks? [board] (valid-groups? (blocks board)))

(defn valid-solution? [board]
nil)
(and (valid-rows? board) (valid-cols? board) (valid-blocks? board)))

(defn set-value-at [board coord new-value]
nil)
(assoc-in board coord new-value))

(defn find-empty-point-helper [board board-coords]
(cond
(empty? board-coords) nil
(not (has-value? board (first board-coords))) (first board-coords)
:else (find-empty-point-helper board (rest board-coords))))

(defn find-empty-point [board]
nil)
(find-empty-point-helper board (coord-pairs (range 9))))


(defn solve-helper [board]
(let [filled (filled? board)]
(cond
(and filled (valid-solution? board)) [board]
filled []
:else (let [coord (find-empty-point board)]
(for [value (valid-values-for board coord)
solution (solve-helper (set-value-at board coord value))]
solution)))))

(defn solve [board]
nil)
(first (solve-helper board)))