Skip to content

Instantly share code, notes, and snippets.

@kylefeng
Created November 30, 2013 08:08
Show Gist options
  • Save kylefeng/7716575 to your computer and use it in GitHub Desktop.
Save kylefeng/7716575 to your computer and use it in GitHub Desktop.
(defn gen-uniq-rand-seq
[n gen-elem-fn]
(loop [xs (java.util.ArrayList.) x (gen-elem-fn)]
(cond
(= n (count xs)) xs
(some #(= % x) xs) (recur xs (gen-elem-fn))
:else (do
(.add xs x)
(recur xs (gen-elem-fn))))))
(defn gen-coordinates
[m n col]
(let [v (vec col)
len (count v)
cor (gen-uniq-rand-seq
len
(fn [] [(rand-int m)
(rand-int n)]))
xs (gen-uniq-rand-seq
len
(fn [] (get v (rand-int len))))
operations (java.util.ArrayList.)]
(dotimes [i len]
(let [c (.get cor i)
x (.get xs i)]
(.add operations [c x])))
operations))
(defn gen-sudoku [m n col]
(let [board (vec (take m (repeat (vec (take n (repeat nil))))))]
(reduce #(assoc-in %1
(first %2)
(second %2))
board (gen-coordinates m n col))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment