Skip to content

Instantly share code, notes, and snippets.

@alun
Created July 13, 2015 11:32
Show Gist options
  • Save alun/6fc1ae46c4d8ec155ec9 to your computer and use it in GitHub Desktop.
Save alun/6fc1ae46c4d8ec155ec9 to your computer and use it in GitHub Desktop.
(fn find-latins
[vectors]
(let [height (count vectors)
width (->> vectors (map count) (apply max))
spaces (for [line vectors]
(-> (- width (count line))
inc
range))
alignments (reduce #(for [cur % up %2]
(conj cur up))
(->> spaces first (map vector))
(next spaces))
alignments (for [alignment alignments]
(for [[ofs-left line] (map vector alignment vectors)
:let [ofs-right (- width ofs-left (count line))]]
(concat
(repeatedly ofs-left #(identity nil))
line
(repeatedly ofs-right #(identity nil)))))
submatrices (for [alignment alignments
n (-> (min width height) inc range)
:when (> n 1)
x (range (- width n -1))
y (range (- height n -1))
:let [subm (->> alignment
(drop y)
(take n)
(map #(->> % (drop x) (take n))))]
:when (not (some nil? (apply concat subm)))]
subm)
latins (for [subm submatrices
:let [n (count subm)
latin-row? #(->> % distinct count (= n))]
:when (and (->> subm
(apply concat)
latin-row?)
(every? latin-row? subm)
(every? latin-row? (apply (partial map vector) subm)))]
subm)]
(->> latins
distinct
(group-by count)
(map (fn [[order latins]]
(vector order (count latins)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment