DEV Community

Discussion on: Daily Challenge #41 - Greed is Good

Collapse
 
craigmc08 profile image
Craig McIlwrath

Haskell:

import Data.Tuple (uncurry)

count :: (a -> Bool) -> [a] -> Int
count pred = length . filter pred

greed :: [Int] -> Int
greed rolls = let groups = [(i, count (==i) rolls) | i <- [1..6]]
                  points3 n = case n of
                    1 -> 1000
                    6 -> 600
                    5 -> 500
                    4 -> 400
                    3 -> 300
                    2 -> 200
                    _ -> 0
                  points1 n = case n of
                    1 -> 100
                    5 -> 50
                    _ -> 0
                  score num count
                    | count == 0 = 0
                    | count >= 3 = points3 num + score num (count - 3)
                    | otherwise = points1 num + score num (count - 1)
              in sum $ map (uncurry score) groups

P.S. Hoogle is really useful. I didn't know about the uncurry function, but I knew I needed a function to do what uncurry does, so I could search for (a -> b -> c) -> (a, b) -> c in hoogle!