Created
March 14, 2018 20:35
-
-
Save damienzhang/3dfa7ee7a910c51d5ede7c91b93f1694 to your computer and use it in GitHub Desktop.
Code for damienzhang.com/numberplay
This file contains hidden or 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
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