Last active
April 11, 2024 09:38
-
-
Save opqdonut/e7bb673a348794c8b8a7f1328152458d to your computer and use it in GitHub Desktop.
a10n.clj
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 a10n | |
(:require [clojure.set :as set] | |
[clojure.string :as str])) | |
(defn i4t [t2e w2d val] | |
(if (empty? w2d) | |
(assoc t2e :end val) | |
(update t2e | |
(first w2d) | |
i4t (rest w2d) val))) | |
(defn b3d [w3s] | |
(reduce #(i4t %1 %2 %2) {} w3s)) | |
(defn e4d [t2e w2d] | |
(cond | |
(empty? w2d) | |
(if (contains? t2e :end) #{(get t2e :end)} #{}) | |
(= \. (first w2d)) | |
(apply set/union (map #(e4d % (rest w2d)) (filter map? (vals t2e)))) | |
:else | |
(e4d (get t2e (first w2d)) (rest w2d)))) | |
(defn c4r [w2d ixs] | |
(apply str (map-indexed #(if (ixs %1) %2 \.) w2d))) | |
(defn s6s? [t2e orig w2d] | |
(letfn [(miss [t2e part] | |
(cond | |
(empty? part) | |
(when-let [e (:end t2e)] (not= orig e)) | |
(= \. (first part)) | |
(some #(miss % (rest part)) (filter map? (vals t2e))) | |
:else | |
(miss (get t2e (first part)) (rest part))))] | |
(not (miss t2e w2d)))) | |
(defn m6e [t2e w2d] | |
(let [unique? (memoize | |
(fn [ixs] | |
(s6s? t2e w2d (c4r w2d ixs)))) | |
best (fn [rec ixs] | |
(let [children (map #(disj ixs %) ixs) | |
uniques (filter unique? children)] | |
(if (empty? uniques) | |
ixs | |
(apply min-key count (doall (map (partial rec rec) uniques))))))] | |
(best (memoize best) (set (range 0 (count w2d)))))) | |
(defn c6s [w2d] | |
(when-not (empty? w2d) | |
(let [[d2s w2d] (split-with #{\.} w2d) | |
[l5s w2d] (split-with (complement #{\.}) w2d)] | |
(str (when (seq d2s) (count d2s)) | |
(apply str l5s) | |
(c6s w2d))))) | |
(defn dec6s [w2d] | |
(when-not (empty? w2d) | |
(let [digit? #{\0 \1 \2 \3 \4 \5 \6 \7 \8 \9} | |
[d4s w2d] (split-with digit? w2d) | |
[l5s w2d] (split-with (complement digit?) w2d)] | |
(str (when (seq d4s) (apply str (repeat (parse-long (apply str d4s)) \.))) | |
(apply str l5s) | |
(dec6s w2d))))) | |
(def w3s (delay (b3d (str/split-lines (slurp "/usr/share/dict/words"))))) | |
(defn a8e [w2d] | |
(->> (m6e @w3s w2d) | |
(c4r w2d) | |
c6s)) | |
(defn una8e [w2d] | |
(->> (dec6s w2d) | |
(e4d @w3s))) | |
(comment | |
(a8e "cybernetics") | |
;; => "cy5t3" | |
(a8e "international") | |
;; => "2t4t4l" | |
(una8e "c9h") | |
;; => #{"cheesecloth" "cottonmouth" "choreograph"} | |
(a8e "choreograph") | |
;; => "1h7p1" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment