Skip to content
Open

Done #441

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
91 changes: 73 additions & 18 deletions src/sudoku.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,110 @@
(def board identity)

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

(defn has-value? [board coord]
nil)
(not (zero? (get-in board coord))))

(defn row-values [board coord]
nil)
(set (get board (get coord 0))))

(defn col-values [board coord]
nil)
(set (map (fn [row] (get row (get coord 1))) board)))

(defn coord-pairs [coords]
nil)
(for [num1 coords
num2 coords]
(vector num1 num2)))

(defn block-row [coord]
(cond
(contains? #{0 1 2} (get coord 0)) #{0 1 2}
(contains? #{3 4 5} (get coord 0)) #{3 4 5}
(contains? #{6 7 8} (get coord 0)) #{6 7 8}))

(defn block-col [coord]
(cond
(contains? #{0 1 2} (get coord 1)) #{0 1 2}
(contains? #{3 4 5} (get coord 1)) #{3 4 5}
(contains? #{6 7 8} (get coord 1)) #{6 7 8}))

(defn block-values [board coord]
nil)
(set (for [num1 (block-row coord)
num2 (block-col coord)]
(value-at board (vector num1 num2)))))

(defn valid-values-for [board coord]
nil)
(if (zero? (value-at board coord))
(set/difference #{1 2 3 4 5 6 7 8 9}
(set/union (row-values board coord)
(col-values board coord)
(block-values board coord)))
(set nil)))

(defn filled? [board]
nil)
(not (contains?
(set (map (fn [coord]
(value-at board coord))
(coord-pairs (range 0 9))))
0)))

(defn rows [board]
nil)
(map (fn [i]
(row-values board [i 0]))
(range 0 9)))

(defn valid-rows? [board]
nil)
(empty? (filter (fn [row]
(not= #{1 2 3 4 5 6 7 8 9} row))
(rows board))))

(defn cols [board]
nil)
(map (fn [i]
(col-values board [0 i]))
(range 0 9)))

(defn valid-cols? [board]
nil)
(empty? (filter (fn [col]
(not= #{1 2 3 4 5 6 7 8 9} col))
(cols board))))

(defn blocks [board]
nil)
(for [num1 [0 3 6]
num2 [0 3 6]]
(block-values board (vector num1 num2))))

(defn valid-blocks? [board]
nil)
(empty? (filter (fn [block]
(not= #{1 2 3 4 5 6 7 8 9} block))
(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 [board]
nil)
(first (filter (fn [coord]
(zero? (value-at board coord)))
(for [num1 (range 0 9)
num2 (range 0 9)]
(vector num1 num2)))))

(defn board-solver [board solutions]
(let [empty-point (find-empty-point board)]
(if empty-point
(for [elem (valid-values-for board empty-point)
solution (board-solver
(set-value-at board empty-point elem)
solutions)]
solution)
(if (valid-solution? board)
(conj solutions board)
solutions))))

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