Created
November 28, 2011 11:13
-
-
Save dmalikov/1400024 to your computer and use it in GitHub Desktop.
Project Euler 205 (0.7 s)
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 Control.Monad (replicateM, join) | |
import Data.List (nub, sort, group) | |
import Control.Arrow ((&&&), (***)) | |
import Numeric (showFFloat) | |
allVariantsNumber :: Integer | |
allVariantsNumber = 4^9 * 6^6 | |
groupVariants :: [[Integer]] -> [(Integer,Integer)] | |
groupVariants = map ((&&&) head (toInteger . length)) . group . sort . map sum | |
peterVariants :: [(Integer, Integer)] | |
peterVariants = groupVariants $ replicateM 9 [1..4] | |
colinVariants :: [(Integer, Integer)] | |
colinVariants = groupVariants $ replicateM 6 [1..6] | |
peterWins :: Integer | |
peterWins = sum [ pVariantNumbers * cVariantNumbers | (pSum, pVariantNumbers) <- peterVariants, (cSum, cVariantNumbers) <- colinVariants, pSum > cSum ] | |
probability :: Double | |
probability = uncurry (/) . join (***) fromIntegral $ (peterWins, allVariantsNumber) | |
main = print $ showFFloat (Just 7) probability "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment