-
-
Save wvdlaan/db774729a56c20c6f759 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns dojo3.core) | |
(def x {:size [10 10] | |
:rows [[] [1 1] [1] [2 1 1] [1 1 1] [1 2 1 1] [1] [1] [] []] | |
:cols [[] [1] [] [3] [1 1] [] [5] 1 [] [1 4 []]]}) | |
(def nono1 {:size [3 3] | |
:rows [[1] [3] [1]] | |
:cols [[1] [3] [1]]}) | |
(def board1 | |
[[0 1 0] | |
[1 1 1] | |
[0 1 0]]) | |
(defn col | |
[board i] | |
(for [j (range (count board))] | |
(get-in board [j i]))) | |
(defn row | |
[board i] | |
(for [j (range (count board))] | |
(get-in board [i j]))) | |
(defn q | |
[coll] | |
(->> coll | |
(partition-by identity) | |
(remove #(zero? (first %))) | |
(map #(apply + %)) | |
vec)) | |
(defn ok? | |
[board nono] | |
(and (every? | |
identity | |
(map #(= (q (col board %)) (get-in nono [:cols %])) | |
(range (count board)))) | |
(every? | |
identity | |
(map #(= (q (row board %)) (get-in nono [:rows %])) | |
(range (count board)))))) | |
(defn init-gaps | |
[rows] | |
(mapv | |
#(vec (repeat (count %) 0)) | |
rows)) | |
(defn row-elem | |
[n gap] | |
(into | |
(vec (repeat gap 0)) | |
(vec (repeat n 1)))) | |
(defn mk-row | |
[no-row gaps] | |
(->> (map row-elem no-row gaps) | |
(interpose [0]) | |
(reduce into []))) | |
(mk-row [2 1 1] [0 0 0]) ;; => [1 1 0 1 0 1] | |
(mk-row [2 1 1] [1 0 0]) ;; => [0 1 1 0 1 0 1] | |
(defn mk-board | |
[rows gaps] | |
(map mk-row | |
rows | |
gaps)) | |
(mk-board | |
(:rows x) | |
(init-gaps (:rows x))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment