all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
41
Task/Sudoku/Clojure/sudoku-1.clj
Normal file
41
Task/Sudoku/Clojure/sudoku-1.clj
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
(ns sudoku
|
||||
(:use [clojure.contrib.math :only (sqrt)]))
|
||||
|
||||
(defn print-grid [grid]
|
||||
(doseq [y (range (count grid))]
|
||||
(doseq [x (range (count grid))]
|
||||
(print (retrieve grid x y) " "))
|
||||
(println))
|
||||
(println))
|
||||
|
||||
(defn retrieve [grid x y]
|
||||
(get (get grid y) x))
|
||||
|
||||
(defn store [grid x y n]
|
||||
(assoc grid y (assoc (get grid y) x n)))
|
||||
|
||||
(defn coordinates [grid x y]
|
||||
(let [n (sqrt (count grid))
|
||||
zx (* n (quot x n))
|
||||
zy (* n (quot y n))]
|
||||
(for [x (range zx (+ zx n)) y (range zy (+ zy n))]
|
||||
[x y])))
|
||||
|
||||
(defn compatible? [grid x y n]
|
||||
(or
|
||||
(= n (retrieve grid x y))
|
||||
(and
|
||||
(zero? (retrieve grid x y))
|
||||
(every? #(and (not= n (retrieve grid % y)) (not= n (retrieve grid x %))) (range (count grid)))
|
||||
(every? #(not= n (retrieve grid (first %) (second %))) (coordinates grid x y)))))
|
||||
|
||||
(defn solve [grid x y]
|
||||
(let [m (count grid)]
|
||||
(if (= y m)
|
||||
(print-grid grid)
|
||||
(doseq [n (range 1 (inc m))]
|
||||
(when (compatible? grid x y n)
|
||||
(let [new-grid (store grid x y n)]
|
||||
(if (= x (dec m))
|
||||
(solve new-grid 0 (inc y))
|
||||
(solve new-grid (inc x) y))))))))
|
||||
21
Task/Sudoku/Clojure/sudoku-2.clj
Normal file
21
Task/Sudoku/Clojure/sudoku-2.clj
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
sudoku> (solve [[3 9 4 0 0 2 6 7 0]
|
||||
[0 0 0 3 0 0 4 0 0]
|
||||
[5 0 0 6 9 0 0 2 0]
|
||||
[0 4 5 0 0 0 9 0 0]
|
||||
[6 0 0 0 0 0 0 0 7]
|
||||
[0 0 7 0 0 0 5 8 0]
|
||||
[0 1 0 0 6 7 0 0 8]
|
||||
[0 0 9 0 0 8 0 0 0]
|
||||
[0 2 6 4 0 0 7 3 5]]
|
||||
0 0)
|
||||
3 9 4 8 5 2 6 7 1
|
||||
2 6 8 3 7 1 4 5 9
|
||||
5 7 1 6 9 4 8 2 3
|
||||
1 4 5 7 8 3 9 6 2
|
||||
6 8 2 9 4 5 3 1 7
|
||||
9 3 7 1 2 6 5 8 4
|
||||
4 1 3 5 6 7 2 9 8
|
||||
7 5 9 2 3 8 1 4 6
|
||||
8 2 6 4 1 9 7 3 5
|
||||
|
||||
nil
|
||||
Loading…
Add table
Add a link
Reference in a new issue