Skip to content

Instantly share code, notes, and snippets.

@wvdlaan
Forked from skuro/nonograms.clj
Last active August 29, 2015 14:11
Show Gist options
  • Save wvdlaan/db774729a56c20c6f759 to your computer and use it in GitHub Desktop.
Save wvdlaan/db774729a56c20c6f759 to your computer and use it in GitHub Desktop.
(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