205 Dice Game

Problem 206 – Project Euler

サイコロ遊び。

目の合計なんて、高々36。

import Data.Function (on)
import Data.Map (Map, fold, fromListWith, filterWithKey,toList)
totals :: [[Integer]] -> Map Integer Integer
totals = fromListWith (+).(`zip` (repeat 1)).map sum.sequence
p205 :: Double
p205 = on (/) fromIntegral wins all
where all = on (*) (fold (+) ) peter colin
wins = sum [win x colin * n | (x,n) <- toList peter]
win x = fold (+) .filterWithKey(k _ -> k < x)
peter = totals $ replicate 9 [1..4]
colin = totals $ replicate 6 [1..6]