Skip to content

Instantly share code, notes, and snippets.

@asolove
Created September 25, 2012 00:47
Show Gist options
  • Save asolove/3779336 to your computer and use it in GitHub Desktop.
Save asolove/3779336 to your computer and use it in GitHub Desktop.
Send more cpu cycles!
(ns learn-logic.core)
(use 'clojure.core.logic)
; an attempt to solve the classic "Send more money" problem, where
; S E N D
; + M O R E
; ----------
; M O N E Y
;
; and both S and M cannot be 0
; A very inefficient way to turn a list of numerals into a number
(defn digits-numbero [nums total]
(fresh [n1 ns]
(conso n1 ns nums)
(conde
((emptyo ns) (== n1 total))
((fresh [n2 nr carry sum newNs]
(conso n2 nr ns)
(*fd n1 10 carry)
(+fd carry n2 sum)
(conso sum nr newNs)
(digits-numbero newNs total))))))
; An attempt to write the solution straightforwardly in terms of the puzzle definition.
(defn -main []
(run 1 [send more]
(fresh [s e n d m o r y money]
(infd s e n d m o r y (interval 0 9))
(distincto [s e n d m o r y])
(!=fd s 0)
(!=fd m 0)
(digits-numbero (list s e n d) send)
(digits-numbero (list m o r e) more)
(digits-numbero (list m o n e y) money)
(+fd send more money))))
; Running this results in OutOfMemoryError.
; I further tried adding:
; (<fd send 10000)
; (<fd more 10000)
; (<fd money 100000)
; With the same result.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment