Created
December 30, 2013 18:30
-
-
Save loganlinn/8185871 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 gumball.core) | |
(defn xy-coords [x-max y-max word-len] | |
(for [x (range (inc (- x-max word-len))) | |
y (range (inc (- y-max word-len)))] | |
(for [n (range word-len)] | |
[(+ x n) (+ y n)]))) | |
(defn yx-coords [x-max y-max word-len] | |
(for [x (range word-len x-max) | |
y (range word-len y-max)] | |
(for [n (range word-len)] | |
[(- x n) (- y n)]))) | |
(defn x-coords [x-max y-max word-len] | |
(for [x (range (inc (- x-max word-len))) | |
y (range y-max)] | |
(for [n (range word-len)] | |
[(+ x n) y]))) | |
(defn y-coords [x-max y-max word-len] | |
(for [x (range x-max) | |
y (range (inc (- y-max word-len)))] | |
(for [n (range word-len)] | |
[x (+ y n)]))) | |
(defn get-word [board coords] | |
(->> coords | |
(map #(get-in board %)) | |
(apply str))) | |
(defn words-by-len [board word-len] | |
(let [x-max (count board) | |
y-max (count (first board))] | |
(map #(get-word board %) | |
(concat (xy-coords x-max y-max word-len) | |
(yx-coords x-max y-max word-len) | |
(x-coords x-max y-max word-len) | |
(y-coords x-max y-max word-len))))) | |
(defn count-matches [board word] | |
(let [rword (apply str (reverse word))] | |
(->> (words-by-len board (count word)) | |
(map #(if (or (= word %) (= rword %)) 1 0)) | |
(reduce +)))) | |
(defn create-board [rows] (mapv vec rows)) | |
(-> (create-board ["GYBLTJZJDDG" | |
"ULNLWVJTMUL" | |
"MLLABMUGMLG" | |
"BAYBGVTBAUG" | |
"ABLMLUABMUL" | |
"LMLUZLMBMLW" | |
"LUAGLUABALV" | |
"GGBRGLABAJK" | |
"RDMTLLMWMLD" | |
"LYUNLUDDMUL" | |
"MQGRGZTWRRG"]) | |
(count-matches "GUMBALL")) ;; => 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment