Created
May 15, 2015 21:42
-
-
Save mitchellwrosen/966fb69c9d2dc88dfa09 to your computer and use it in GitHub Desktop.
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
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