SHARE
TWEET

Untitled

a guest May 28th, 2015 241 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns doublets.solver
  2.   (:require [clojure.java.io :as io]
  3.             [clojure.edn :as edn]
  4.             [clojure.data.priority-map :refer :all]))
  5.  
  6. ;(def words (-> "words.edn"
  7.  ;              (io/resource)
  8.  ;              (slurp)
  9.  ;              (read-string)))
  10.  
  11. (def words [ "door"
  12.   "boor"
  13.   "cook"
  14.   "book"
  15.   "look"
  16.   "lock"])
  17.  
  18. (defn dist [word1 word2 & {:keys [n] :or {n 0}}]
  19.   ; Assumes lengths of word1 and word2 are the same
  20.   (let [len1 (count word1)
  21.         len2 (count word2)]
  22.     (if (= 0 len1) n
  23.       (if (= 0 len2) n
  24.         (if (or (= 1 len1) (= 1 len2))
  25.           (if (= word1 word2)
  26.             n
  27.             (+ n 1))
  28.           (let [c1 (subs word1 0 1)
  29.                 c2 (subs word2 0 1)
  30.                 r1 (subs word1 1)
  31.                 r2 (subs word2 1)]
  32.             (if (= c1 c2)
  33.               (dist r1 r2 :n n)
  34.               (dist r1 r2 :n (+ n 1)))))))))
  35.  
  36.  
  37.  
  38. (defn futureCost [goal]
  39.   (reduce (fn [pm word]
  40.             (assoc pm (keyword word) (dist word goal)))
  41.           (priority-map)
  42.           (filter #(= (count %) (count goal)) words)))
  43.  
  44.  
  45. (defn doublets [word1 goal]
  46.   (loop [result [word1]
  47.          current-word word1
  48.          cand-words (map name (keys (futureCost goal)))]
  49.     (if (= current-word goal)
  50.       result
  51.       (let [w (filter
  52.                #(= 1 (dist % current-word))
  53.                cand-words)]
  54.         (recur (conj result w)
  55.                w
  56.                (remove #(= % w) cand-words))))))
  57.      
  58.  
  59. (defn -main []
  60.  
  61.   (print (doublets "door" "lock") "\n"))
  62. ;  (print (keys (futureCost "lock")) "\n"))
  63. ;  (print (futureCost "lock") "\n"))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top