4
(ns verbal-arithmetic
  (:require
    [clojure.core.logic :refer [all run* everyg lvar == membero fresh conde succeed fail conso resto]]
    [clojure.core.logic.fd :as fd]))

(comment
  "Solving cryptarithmetic puzzle"
  " SEND
  + MORE
  ______
   MONEY")


(defn send-more-money-solutions []
  (run* [s e n d m o r y]
        (fd/in s e n d m o r y (fd/interval 0 9))
        (fd/!= s 0)
        (fd/!= m 0)
        (fd/distinct [s e n d m o r y])
        (fd/eq (= (apply + [(* 1000 s) (* 100 e) (* 10 n) d
                            (* 1000 m) (* 100 o) (* 10 r) e])
                  (apply + [(* 10000 m) (* 1000 o) (* 100 n) (* 10 e) y])))))

上面的示例不起作用,因为applyfd/eq. 以下版本send-more-money-solutions有效,因为我不使用apply. 我需要使用apply泛化解决方案来处理不同长度的任意字符串。

(defn send-more-money-solutions []
  (run* [s e n d m o r y]
        (fd/in s e n d m o r y (fd/interval 0 9))
        (fd/!= s 0)
        (fd/!= m 0)
        (fd/distinct [s e n d m o r y])
        (fd/eq (= (+ (* 1000 s) (* 100 e) (* 10 n) d
                     (* 1000 m) (* 100 o) (* 10 r) e)
                  (+ (* 10000 m) (* 1000 o) (* 100 n) (* 10 e) y)))))

我应该怎么办?(对于上面,我有一个想法,我可以编写一个宏(虽然还不确定如何),但实际上我需要能够使用作为逻辑变量序列的变量。如下所示)

(fd/eq (= (+ (apply + lvars1) (apply + lvars2))
          (apply + lvars3)))

错误消息看起来像

java.lang.IllegalArgumentException: Can't call nil, form: (nil + [(* 1000 s) (* 100 e) (* 10 n) d (* 1000 m) (* 100 o) (* 10 r) e] G__1124704)

我认为宏中发生了一些奇怪的事情,fd/eq所以我应该尝试不使用eq宏。

谢谢大家!

4

1 回答 1

5

我需要能够使用作为逻辑变量序列的变量

确切地说,该问题的一般解决方案是引入任意动态数量的逻辑变量并关联/约束它们。

求解器

首先定义一些递归目标来处理逻辑变量序列。(幸运的是,我已经为以前的问题准备了这些!)

  1. 将一系列逻辑变量的总和与另一个逻辑变量相关联:

    (defn sumo [vars sum]
      (fresh [vhead vtail run-sum]
        (conde
          [(== vars ()) (== sum 0)]
          [(conso vhead vtail vars)
           (fd/+ vhead run-sum sum)
           (sumo vtail run-sum)])))
    
  2. 将两个逻辑变量序列的乘积之和与另一个逻辑变量相关联:

    (defn productsumo [vars dens sum]
      (fresh [vhead vtail dhead dtail product run-sum]
        (conde
          [(emptyo vars) (== sum 0)]
          [(conso vhead vtail vars)
           (conso dhead dtail dens)
           (fd/* vhead dhead product)
           (fd/+ product run-sum sum)
           (productsumo vtail dtail run-sum)])))
    

加上一个小辅助函数来生成幅度乘数:

(defn magnitudes [n]
  (reverse (take n (iterate #(* 10 %) 1))))

然后将它们连接在一起:

(defn cryptarithmetic [& words]
  (let [distinct-chars (distinct (apply concat words))
        char->lvar (zipmap distinct-chars (repeatedly (count distinct-chars) lvar))
        lvars (vals char->lvar)
        first-letter-lvars (distinct (map #(char->lvar (first %)) words))
        sum-lvars (repeatedly (count words) lvar)
        word-lvars (map #(map char->lvar %) words)]
    (run* [q]
      (everyg #(fd/in % (fd/interval 0 9)) lvars) ;; digits 0-9
      (everyg #(fd/!= % 0) first-letter-lvars) ;; no leading zeroes
      (fd/distinct lvars) ;; only distinct digits
      (everyg (fn [[sum l]] ;; calculate sums for each word
                (productsumo l (magnitudes (count l)) sum))
              (map vector sum-lvars word-lvars))
      (fresh [s]
        (sumo (butlast sum-lvars) s) ;; sum all input word sums
        (fd/== s (last sum-lvars)))  ;; input word sums must equal last word sum
      (== q char->lvar))))

从您的示例中,其中一些应该看起来很熟悉,但主要区别在于可以动态处理单词(及其字符)的数量。lvar为所有字符的集合以及每个单词的总和创建了新的逻辑变量。everyg然后使用上面的递归目标来约束/关联逻辑变量。

示例问题

该函数将返回给定单词的所有解决方案,而“发送更多钱”只有一种可能的解决方案:

(cryptarithmetic "send" "more" "money")
=> ({\s 9, \e 5, \n 6, \d 7, \m 1, \o 0, \r 8, \y 2})

另一个有四个单词的例子是“cp is fun true”(参见Google Cryptarithmetic Puzzles),它有 72 种可能的解决方案:

(cryptarithmetic "cp" "is" "fun" "true")
=>
({\c 2, \e 4, \f 9, \i 7, \n 3, \p 5, \r 0, \s 6, \t 1, \u 8}
 {\c 2, \e 5, \f 9, \i 7, \n 3, \p 4, \r 0, \s 8, \t 1, \u 6}
 {\c 2, \e 6, \f 9, \i 7, \n 3, \p 5, \r 0, \s 8, \t 1, \u 4}
 ...

这是我能在Wikipedia上找到的最大的一个,该函数在我的笔记本电脑上找到了大约 30 秒的唯一解决方案:

(cryptarithmetic "SO" "MANY" "MORE" "MEN" "SEEM" "TO"
                 "SAY" "THAT" "THEY" "MAY" "SOON" "TRY"
                 "TO" "STAY" "AT" "HOME" "SO" "AS" "TO"
                 "SEE" "OR" "HEAR" "THE" "SAME" "ONE"
                 "MAN" "TRY" "TO" "MEET" "THE" "TEAM"
                 "ON" "THE" "MOON" "AS" "HE" "HAS"
                 "AT" "THE" "OTHER" "TEN" "TESTS")
=> ({\A 7, \E 0, \H 5, \M 2, \N 6, \O 1, \R 8, \S 3, \T 9, \Y 4})

这是一个漂亮地打印结果的函数:

(defn pprint-answer [char->digit words]
  (let [nums (map #(apply str (map char->digit %))
                  words)
        width (apply max (map count nums))
        width-format (str "%" width "s")
        pad #(format width-format %)]
    (println
     (clojure.string/join \newline
       (concat
        (map #(str "+ " (pad %)) (butlast nums))
        [(apply str (repeat (+ 2 width) \-))
         (str "= " (pad (last nums)))]))
     \newline)))

(cryptarithmetic "wrong" "wrong" "right")
(map #(pprint-answer % ["wrong" "wrong" "right"]) *1)
; + 12734
; + 12734
; -------
; = 25468 
于 2019-03-23T22:42:06.323 回答