Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@LeventErkok
Created May 13, 2015 00:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeventErkok/49347cc0b96d72efe17c to your computer and use it in GitHub Desktop.
Save LeventErkok/49347cc0b96d72efe17c to your computer and use it in GitHub Desktop.
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]
@FranklinChen
Copy link

The "right tool for the job" :-).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment