Hello everybody !

I just start learning Clojure and I am a complete newbie !

I have tried to solve this classical small puzzle with Clojure.
Assign one digit from {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} to each letter {o n e 
t h r l v w y} so that
one + one + one + three + three + eleven = twenty

Here is the code:

(defn permutations [s]
  (lazy-seq
   (if (seq (rest s))
     (apply concat (for [x s]
                     (map #(cons x %) (permutations (remove #{x} s)))))
     [s])))

(defn tomap [proposal]
    (zipmap [:o :n :e :t :h :r :l :v :w :y] proposal))

(defn one-value [m]
    (+ (* 100 (:o m))
       (*  10 (:n m))
              (:e m)))

(defn three-value [m]
    (+ (* 10000 (:t m))
       (*  1000 (:h m))
       (*   100 (:r m))
       (*    10 (:e m))
                (:e m)))

(defn eleven-value [m]
    (+ (* 100000 (:e m))
       (*  10000 (:l m))
       (*   1000 (:e m))
       (*    100 (:v m))
       (*     10 (:e m))
                 (:n m)))

(defn twenty-value [m]
    (+ (* 100000 (:t m))
       (*  10000 (:w m))
       (*   1000 (:e m))
       (*    100 (:n m))
       (*     10 (:t m))
                 (:y m)))

(defn check? [proposal]
    (let [m (tomap proposal)
          one (one-value m)
          three (three-value m)
          eleven (eleven-value m)
          twenty (twenty-value m)]

        (= (+ one one one three three eleven)
           twenty)))

(defn print-solution [solution]
    (println (tomap solution)))

(doall (map print-solution (filter check? (permutations (range 10)))))

This program prints the correct solution, but I have some questions !
It seems to consume lot of memory (around 6GB). Do I miss something with 
the lazy evaluation ?
How can I change the code into a more idiomatic one ?

Thanks for your help and time !

Olivier


-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to