Skip to content

Instantly share code, notes, and snippets.

@athos
Created June 25, 2013 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save athos/5858819 to your computer and use it in GitHub Desktop.
Save athos/5858819 to your computer and use it in GitHub Desktop.
A solution in Clojure for the problem of SEND + MORE = MONEY
(ns send-more-money
(:require [clojure.math.combinatorics :refer [combinations permutations]]))
(defn solve [n cond]
(for [ds (combinations (range 10) n)
ds' (permutations ds)
:when (cond ds')]
ds'))
(defn ->n [ds]
(reduce #(+ (* 10 %1) %2) ds))
(defn send-more-money [[s e n d m o r y]]
(and (not= s 0) (not= m 0)
(= (+ (->n [s e n d]) (->n [m o r e]))
(->n [m o n e y]))))
; (solve 8 send-more-money)
(defn send-more-gold-money [[s e n d m o r g l y]]
(and (not= s 0) (not= m 0) (not= g 0)
(= (+ (->n [s e n d]) (->n [m o r e]) (->n [g o l d]))
(->n [m o n e y]))))
; (solve 10 send-more-gold-money)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment