Skip to content

Instantly share code, notes, and snippets.

@itarato
Created June 1, 2015 20:30
Show Gist options
  • Save itarato/a7bf032e7d343f6d0674 to your computer and use it in GitHub Desktop.
Save itarato/a7bf032e7d343f6d0674 to your computer and use it in GitHub Desktop.
Mushroom eater puzzle
import System.IO
import Control.Monad
solve_v1 :: [Int] -> Int
solve_v1 (a:[]) = 0
solve_v1 (a:b:c) = (if a > b then a - b else 0) + (solve_v1 (b:c))
min_dist :: [Int] -> Int
min_dist (a:[]) = 0
min_dist (a:b:c) = if a > b then max (min_dist (b:c)) (a - b) else min_dist (b:c)
solve_v2 :: [Int] -> Int -> Int
solve_v2 (a:[]) _ = 0
solve_v2 (a:b:c) d = (min a d) + (solve_v2 (b:c) d)
main = do
fin <- openFile "mushroom.in" ReadMode
examples <- hGetLine fin
forM_ (replicate (read examples :: Int) fin) $ \handle -> do
_ <- hGetLine handle
input <- hGetLine handle
items <- return $ map (\a -> read a :: Int) $ words input
print $ solve_v1 items
print $ solve_v2 items (min_dist items)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment