Skip to content

Instantly share code, notes, and snippets.

@bhugueney
Created April 14, 2016 15:28
Show Gist options
  • Save bhugueney/a3572519a066c7b3c011ab32c2b0e3c1 to your computer and use it in GitHub Desktop.
Save bhugueney/a3572519a066c7b3c011ab32c2b0e3c1 to your computer and use it in GitHub Desktop.
arithmetic constraints puzzle with Clojure / Loco
;; 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