Created
May 13, 2015 00:25
-
-
Save LeventErkok/49347cc0b96d72efe17c to your computer and use it in GitHub Desktop.
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
module SendMoreMoney where | |
import Data.SBV | |
-- | | |
-- >>> sendMoreMoney | |
-- Solution #1: | |
-- s = 9 :: Integer | |
-- e = 5 :: Integer | |
-- n = 6 :: Integer | |
-- d = 7 :: Integer | |
-- m = 1 :: Integer | |
-- o = 0 :: Integer | |
-- r = 8 :: Integer | |
-- y = 2 :: Integer | |
-- This is the only solution. | |
-- | |
-- That is: | |
-- | |
-- >>> 9567 + 1085 == 10652 | |
-- True | |
sendMoreMoney :: IO AllSatResult | |
sendMoreMoney = allSat $ do | |
ds@[s,e,n,d,m,o,r,y] <- mapM sInteger ["s", "e", "n", "d", "m", "o", "r", "y"] | |
let isDigit x = x .>= 0 &&& x .<= 9 | |
val xs = sum $ zipWith (*) (reverse xs) (iterate (*10) 1) | |
send = val [s,e,n,d] | |
more = val [m,o,r,e] | |
money = val [m,o,n,e,y] | |
constrain $ bAll isDigit ds | |
constrain $ allDifferent ds | |
constrain $ s ./= 0 &&& m ./= 0 | |
solve [send + more .== money] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The "right tool for the job" :-).