Skip to content

Instantly share code, notes, and snippets.

@vxgmichel
Created July 10, 2018 16:03
Show Gist options
  • Save vxgmichel/5f3cfc69e06dd149d6bc188a58f949cd to your computer and use it in GitHub Desktop.
Save vxgmichel/5f3cfc69e06dd149d6bc188a58f949cd to your computer and use it in GitHub Desktop.
Solve the 'Infinite House of Pancakes' problem
-- Solve the 'Infinite House of Pancakes' problem.
-- https://code.google.com/codejam/contest/6224486/dashboard#s=p1
import Text.Printf
-- Generic solver code
main :: IO ()
main = interact
$ unlines . cases . map (format . solve) . parse . tail . lines
where cases = zipWith (printf "Case #%d: %s") [1::Int ..]
-- Problem-specific code
parse :: [String] -> [[Int]]
parse [] = []
parse (_:x:xs) = (map read . words $ x) : parse xs
solve :: [Int] -> Int
solve s = minimum [test l s | l <- [1 .. maximum s]]
where test l = (+l) . sum . map (cost l)
where cost l = (`div` l) . subtract 1
format :: Int -> String
format = show
@Gurkenglas
Copy link

Gurkenglas commented Jul 11, 2018

We can whittle closer to parse's varying core:

main :: IO ()
main = interact
  $ unlines . cases . map (format . solve) . parse parser . tail . lines
  where cases = zipWith (printf "Case #%d: %s") [1::Int ..]
        parse = unfoldr . runStateT . ($ StateT uncons)

parser :: Monad m => m String -> m [Int]
parser line = do line; map read . words <$> line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment