Skip to content

Instantly share code, notes, and snippets.

@msszczep
Last active Apr 1, 2018
Embed
What would you like to do?
Scramboni in Clojure
(defn scramboni [rack]
"Find all playable words in a given Scrabble rack. Use asterisks for blanks."
(letfn [(every-but-n? [n pred coll]
(->> coll
(map (comp (fn [x] (if x :true :false)) pred))
frequencies
(merge {:false 0})
:false
(>= n)))]
(let [scrabble-words (->> "/home/mitchells/Desktop/npr_sunday_puzzle_solutions/resources/ospd3.txt"
slurp
clojure.string/split-lines)
rack-frequencies (frequencies rack)
template (zipmap "abcdefghijklmnopqrstuvwxyz" (repeat 26 0))
points {\a 1 \b 3 \c 3 \d 2 \e 1 \f 4 \g 2 \h 4 \i 1 \j 8 \k 5 \l 1 \m 3
\n 1 \o 1 \p 3 \q 10 \r 1 \s 1 \t 1 \u 1 \v 4 \w 4 \x 8 \y 4 \z 10}
num-asterisks (if-let [n (rack-frequencies \*)] n 0)
frequencies-to-use (merge template rack-frequencies)
filtered-words (filter
(fn [word] (every-but-n? num-asterisks
#(<= ((frequencies word) %)
(frequencies-to-use %))
(seq word)))
scrabble-words)
filtered-word-scores (map #(reduce + (map points (seq %))) filtered-words)]
(->> (zipmap filtered-words filtered-word-scores)
(sort-by val)
reverse))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment