Created
June 25, 2013 14:20
-
-
Save athos/5858819 to your computer and use it in GitHub Desktop.
A solution in Clojure for the problem of SEND + MORE = MONEY
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
(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