Skip to content

Instantly share code, notes, and snippets.

@DeBukkIt
Created April 1, 2021 09:19
Show Gist options
  • Save DeBukkIt/66d89521ed396fe4a19700ec5a3f9477 to your computer and use it in GitHub Desktop.
Save DeBukkIt/66d89521ed396fe4a19700ec5a3f9477 to your computer and use it in GitHub Desktop.
Solve SEND+MORE=MONEY puzzle with Haskell
main = do
print "(s,e,n,d,m,o,r,y)"
print solveSendMoreMoney
-- S E N D
-- + M O R E
-- ---------
-- M O N E Y
-- where M /= 0 and every digit stands for a different (unique) number
solveSendMoreMoney = head [(s,e,n,d,m,o,r,y) |
s <- [0..9],
e <- [0..9],
n <- [0..9],
d <- [0..9],
m <- [1..9], -- [sic!]
o <- [0..9],
r <- [0..9],
y <- [0..9],
unique (s:e:n:d:m:o:r:y:[]),
s * 1000 + e * 100 + n * 10 + d +
m * 1000 + o * 100 + r * 10 + e ==
m * 10000 + o * 1000 + n * 100 + e * 10 + y]
unique [] = True
unique [x] = True
unique (x:xs) = not (x `elem` xs) && unique xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment