Skip to content

Instantly share code, notes, and snippets.

@delexi
Created December 25, 2014 20:32
Show Gist options
  • Save delexi/f4e0411183ddf353387e to your computer and use it in GitHub Desktop.
Save delexi/f4e0411183ddf353387e to your computer and use it in GitHub Desktop.
(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 (-> available
(map (fn [x] [x (diff from x)]))
(filter #(= (% 0) 1))
(map #(% 1)))]
(apply concat
(map
#(doublets-rec % (conj visited %)
(sets/difference available %) to)
neighbours)))))]
(doublets-rec word1 [] (into #{} words) word2)))
(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