aboutsummaryrefslogtreecommitdiff
path: root/haskell/wip/205-dice_game.hs
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-09-04 14:51:49 +0200
committerCharles <sircharlesaze@gmail.com>2019-09-04 14:51:49 +0200
commit90ee38953d70b66aa78b5d09da53a63d3dba9f65 (patch)
tree6be4aea9cfac5908ad9e41304f112ece4b2249ba /haskell/wip/205-dice_game.hs
parent08fddd1df75d4d9c76e7fa2e43c157232748abb6 (diff)
downloadproject_euler-90ee38953d70b66aa78b5d09da53a63d3dba9f65.tar.gz
project_euler-90ee38953d70b66aa78b5d09da53a63d3dba9f65.tar.bz2
project_euler-90ee38953d70b66aa78b5d09da53a63d3dba9f65.zip
problem 044 haskell, some wip
Diffstat (limited to 'haskell/wip/205-dice_game.hs')
-rw-r--r--haskell/wip/205-dice_game.hs35
1 files changed, 35 insertions, 0 deletions
diff --git a/haskell/wip/205-dice_game.hs b/haskell/wip/205-dice_game.hs
new file mode 100644
index 0000000..d21623c
--- /dev/null
+++ b/haskell/wip/205-dice_game.hs
@@ -0,0 +1,35 @@
+-- Dice Game
+--
+-- Problem 205
+-- Peter has nine four-sided (pyramidal) dice, each with faces numbered 1, 2, 3, 4.
+-- Colin has six six-sided (cubic) dice, each with faces numbered 1, 2, 3, 4, 5, 6.
+--
+-- Peter and Colin roll their dice and compare totals: the highest total wins. The
+-- result is a draw if the totals are equal.
+--
+-- What is the probability that Pyramidal Pete beats Cubic Colin? Give your answer
+-- rounded to seven decimal places in the form 0.abcdefg
+
+
+import System.Random
+import Data.List
+
+main = do
+ gen <- getStdGen
+ let n = 20000
+ peter = map sum $ splitIndex 9 $ randN (1, 4) gen (9 * n)
+ colin = map sum $ splitIndex 6 $ randN (1, 6) gen (6 * n)
+ let score = length $ filter (\(p, c) -> p > c) (zip peter colin)
+ print ((fromIntegral score) / (fromIntegral n))
+
+splitIndex :: Int -> [Int] -> [[Int]]
+splitIndex n xs
+ | length xs < n = []
+ | otherwise = h : splitIndex n t
+ where (h, t) = splitAt n xs
+
+randN :: (Int, Int) -> StdGen -> Int -> [Int]
+randN _ _ 0 = []
+randN r gen n = fst next : randN r (snd next) (n - 1)
+ where next = randomR r gen :: (Int, StdGen)
+