Create a gist now

Instantly share code, notes, and snippets.

Embed
Code for damienzhang.com/numberplay
import Data.List
import Text.Printf
choose :: Int -> [a] -> [[a]]
choose 0 _ = [[]]
choose _ [] = []
choose n (x:xs) = map (x :) (choose (n - 1) xs) ++ choose n xs
perm :: Int -> [a] -> [[a]]
perm n = concatMap permutations . choose n
candidates :: [([Char], [Char])]
candidates = map (splitAt 3) . perm 6 $ ['0' .. '9']
solutions = filter f candidates
where
f (num, ber) =
all (('0' /=) . head) [num, ber, play] &&
allDistinct (num ++ ber ++ play)
where
play = printf "%04d" (read num + read ber :: Int)
allDistinct xs = length xs == length (nub xs)
main = do
putStrLn $
let (num, ber) = head solutions
in printf "One solution: %s + %s = %d" num ber (read num + read ber :: Int)
putStrLn $ printf "Solutions: %d" (length solutions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment