Created
April 14, 2016 15:28
-
-
Save bhugueney/a3572519a066c7b3c011ab32c2b0e3c1 to your computer and use it in GitHub Desktop.
arithmetic constraints puzzle with Clojure / Loco
This file contains 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
;; cf https://github.com/aengelberg/loco/blob/master/test/loco/integer/sendmoremoney1.clj | |
(ns constrained-grouping.core | |
(:use loco.constraints | |
loco.core)) | |
(defn initialize-digits [vars] | |
(for [v vars] | |
($in v 0 9))) | |
(def letters | |
[:o :n :e :t :h :r :l :v :w :y]) | |
(def puzzle-model | |
(concat (initialize-digits letters) | |
[($distinct letters) | |
($> :o 0) | |
($> :t 0) | |
($> :e 0) | |
($= ($+ ($* 3 ($scalar [:o :n :e] [100 10 1])) | |
($* 2 ($scalar [:t :h :r :e :e] [10000 1000 100 10 1])) | |
($scalar [:e :l :e :v :e :n] [100000 10000 1000 100 10 1])) | |
($scalar [:t :w :e :n :t :y] [100000 10000 1000 100 10 1]))])) | |
(time (solutions puzzle-model)) | |
;; "Elapsed time: 61.496745 msecs" | |
;; ({:y 8, :r 7, :v 1, :o 2, :n 3, :w 4, :e 5, :l 0, :h 9, :t 6}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment