Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn try-change [i w [u v]]
- (loop [w w, u u]
- (let [[hw tw] ((juxt peek pop) w)
- [hu tu] ((juxt first rest) u)]
- (cond (nil? hu) [(inc i) (reduce conj w v)]
- (nil? hw) nil
- (= hw hu) (recur tw tu)
- :else nil))))
- (defn new-word [system i word]
- (first (keep (partial try-change i word) system)))
- (defn run [path word]
- (let [system (read-psy path)]
- (as-> word $
- (vector 0 (reduce conj clojure.lang.PersistentQueue/EMPTY $))
- (iterate (fn [[i w]] (new-word system i w)) $)
- (take-while identity $)
- ((fn [ws] [(take 1000 ws) (last ws)]) $)
- ((fn [[ws [i w]]] [ws i (apply str w)]) $))))
- (defn try-change* [w [u v]]
- (loop [w w, u u]
- (let [[hw tw] ((juxt peek pop) w)
- [hu tu] ((juxt first rest) u)]
- (cond (nil? hu) (reduce conj w v)
- (nil? hw) nil
- (= hw hu) (recur tw tu)
- :else nil))))
- (defn new-word* [system word]
- (first (keep (partial try-change* word) system)))
- (defn run* [system word]
- (let [system' (read-psy system)
- w' (reduce conj clojure.lang.PersistentQueue/EMPTY word)]
- (loop [w w', ws [], n 1000, i 0]
- (let [nw (new-word* system' w)
- nws (if (> n 0) (conj ws (str i ": " (apply str w))) ws)]
- (if (nil? nw)
- [nws (dec i) (apply str w)]
- (recur nw nws (dec n) (inc i)))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement