Advertisement
Average-user

Post

Apr 14th, 2018
1,270
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn try-change [i w [u v]]
  2.   (loop [w w, u u]
  3.     (let [[hw tw] ((juxt peek pop) w)
  4.           [hu tu] ((juxt first rest) u)]
  5.       (cond (nil? hu) [(inc i) (reduce conj w v)]
  6.             (nil? hw) nil
  7.             (= hw hu) (recur tw tu)
  8.             :else     nil))))
  9.  
  10. (defn new-word [system i word]
  11.   (first (keep (partial try-change i word) system)))
  12.  
  13. (defn run [path word]
  14.   (let [system (read-psy path)]
  15.     (as-> word $
  16.       (vector 0 (reduce conj clojure.lang.PersistentQueue/EMPTY $))
  17.       (iterate (fn [[i w]] (new-word system i w)) $)
  18.       (take-while identity $)
  19.       ((fn [ws] [(take 1000 ws) (last ws)]) $)
  20.       ((fn [[ws [i w]]] [ws i (apply str w)]) $))))
  21.  
  22. (defn try-change* [w [u v]]
  23.   (loop [w w, u u]
  24.     (let [[hw tw] ((juxt peek pop) w)
  25.           [hu tu] ((juxt first rest) u)]
  26.       (cond (nil? hu) (reduce conj w v)
  27.             (nil? hw) nil
  28.             (= hw hu) (recur tw tu)
  29.             :else     nil))))
  30.  
  31. (defn new-word* [system word]
  32.   (first (keep (partial try-change* word) system)))
  33.  
  34. (defn run* [system word]
  35.   (let [system' (read-psy system)
  36.         w'      (reduce conj clojure.lang.PersistentQueue/EMPTY word)]
  37.     (loop [w w', ws [], n 1000, i 0]
  38.       (let [nw  (new-word* system' w)
  39.             nws (if (> n 0) (conj ws (str i ": " (apply str w))) ws)]
  40.         (if (nil? nw)
  41.           [nws (dec i) (apply str w)]
  42.           (recur nw nws (dec n) (inc i)))))))
Advertisement
RAW Paste Data Copied
Advertisement