Last active
November 7, 2018 21:31
-
-
Save M4GNV5/14f7933830ec97b552fe7dfc3dccbb83 to your computer and use it in GitHub Desktop.
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
import Data.Ord | |
import Data.List | |
import Data.Function | |
dice n = [1..n] | |
combinations xs ys = concat $ map (\y -> map (\x -> x : y) xs) ys | |
diceCombinations n k = foldr (combinations) (map return diceN) (replicate (k - 1) diceN) | |
where | |
diceN = dice n | |
mapFst f xs = map (\(x, y) -> (f x, y)) xs | |
isEq :: Ordering -> Bool | |
isEq EQ = True | |
isEq _ = False | |
elementCounts :: (a -> a -> Ordering) -> [a] -> [(a, Int)] | |
elementCounts f xs = map (\ys -> (head ys, length ys)) $ groupBy (\x y -> isEq $ f x y) $ sortBy f xs | |
diceModuloCounts k = groupModCounts | |
where | |
diceCounts = elementCounts compare $ map sum $ diceCombinations 6 k | |
modCounts = mapFst (`rem`k) diceCounts | |
groupedMods = groupBy ((==) `on` fst) $ sortBy (compare `on` fst) modCounts | |
groupModCounts = map (\l -> (fst $ head l, sum $ map snd l)) groupedMods |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment