Skip to content

Instantly share code, notes, and snippets.

@mitchellwrosen
Created May 15, 2015 21:42
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 mitchellwrosen/966fb69c9d2dc88dfa09 to your computer and use it in GitHub Desktop.
Save mitchellwrosen/966fb69c9d2dc88dfa09 to your computer and use it in GitHub Desktop.
send + more = money
module SendMoreMoney where
import Control.Monad
import Control.Monad.Trans.State
digits :: [Int]
digits = [0..9]
select :: StateT [a] [] a
select = StateT go
where
go :: [a] -> [(a, [a])]
go [] = []
go (x:xs) = (x,xs) : do
(y,ys) <- go xs
return (y,x:ys)
val :: [Int] -> Int
val = foldl (\acc n -> acc*10 + n) 0
-- S E N D
-- + M O R E
-- ---------
-- M O N E Y
sendMoreMoney :: StateT [Int] [] (Int, Int, Int)
sendMoreMoney = do
s <- select
e <- select
n <- select
d <- select
m <- select
o <- select
r <- select
y <- select
let send = val [s,e,n,d]
more = val [m,o,r,e]
money = val [m,o,n,e,y]
guard (s /= 0)
guard (m /= 0)
guard (send + more == money)
return (send, more, money)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment