Last active
August 29, 2015 13:56
-
-
Save pwightman/8968972 to your computer and use it in GitHub Desktop.
Letterpress Solver
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 letterpress-solver.core | |
(:require [clojure.string :as string])) | |
(def letters "srrbt | |
sdofa | |
ghvyp | |
ksbkk | |
begnv") | |
(def letterpress-wordlist (string/split-lines (slurp "resources/wordlist.txt"))) | |
(defn remove-first | |
"If `el` is found in `seek`, returns a list with the first occurrence removed. | |
If `el` is not in `seek`, returns `seek`." | |
[seek el] | |
(loop [before '[] | |
after (vec seek)] | |
(cond | |
(empty? after) seek | |
(= el (first after)) (concat before (rest after)) | |
:else (recur (conj before (first after)) (rest after))))) | |
(defn contains-word? | |
"If all letters in `word` can be found in `letters`, returns true, otherwise false" | |
[letters word] | |
(loop [letters-left letters | |
word-left word] | |
(let [new-letters-left (remove-first letters-left (first word-left))] | |
(cond | |
(empty? word-left) true | |
(= (count letters-left) (count new-letters-left)) false | |
:else (recur new-letters-left (rest word-left)))))) | |
(defn -main [& args] | |
(println "Began...") | |
(def words (reduce (fn [found-words word] | |
(if (contains-word? letters word) | |
(conj found-words word) | |
found-words)) | |
'() | |
letterpress-wordlist)) | |
(println "Sorting...") | |
(println (sort-by count words))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment