Created
December 25, 2014 20:52
-
-
Save delexi/0ca1ba9ebd74c4ebf63f 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 doublets.solver | |
(:require [clojure.java.io :as io] | |
[clojure.edn :as edn] | |
[clojure.set :as sets])) | |
(def words (-> "words.edn" | |
(io/resource) | |
(slurp) | |
(read-string))) | |
(defn doublets [word1 word2] | |
(letfn [(doublets-rec [from visited available to] | |
(cond | |
(= from to) visited | |
(empty? available) [] | |
:else (let [neighbours ] | |
(apply concat | |
(map | |
#(doublets-rec % (conj visited %) | |
(sets/difference available %) to) | |
()neighbours)))))] | |
(doublets-rec word1 [] (into #{} words) word2))) | |
(defn neighbours [coll colls distance] | |
(->> colls | |
(map (fn [x] [x (diff coll x)])) | |
(filter #(<= 1 (% 0) distance)) | |
(map #(% 1)))) | |
(defn diff [w1 w2] | |
"Positionally compares the given collections and returns the number of differing elements." | |
(+ (Math/abs (- (count w1) (count w2))) | |
(count (filter #(not (= (% 0) (% 1))) (map #(into [] %&) w1 w2))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment