From f43c0a8ca58459dc5c3ac135c89d82d8562a828f Mon Sep 17 00:00:00 2001 From: Jacob Lagares Pozo Date: Fri, 3 Dec 2021 20:17:11 +0100 Subject: [PATCH] General improvements. --- advlib.cabal | 1 + package.yaml | 1 + src/Advent.hs | 28 +++++++++++++++------------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/advlib.cabal b/advlib.cabal index b5c9b74..6bd1d43 100644 --- a/advlib.cabal +++ b/advlib.cabal @@ -33,4 +33,5 @@ library ghc-options: -threaded -rtsopts -with-rtsopts=-N build-depends: base >=4.7 && <5 + , strict default-language: Haskell2010 diff --git a/package.yaml b/package.yaml index f3329da..87373eb 100644 --- a/package.yaml +++ b/package.yaml @@ -21,6 +21,7 @@ description: Simple boilerplate for handling I/O in advent of code solutions. dependencies: - base >= 4.7 && < 5 +- strict library: source-dirs: src diff --git a/src/Advent.hs b/src/Advent.hs index b4d81eb..5e743a8 100644 --- a/src/Advent.hs +++ b/src/Advent.hs @@ -1,7 +1,14 @@ module Advent where +import System.CPUTime (getCPUTime) import System.Environment (getArgs) import System.IO +import qualified System.IO.Strict as S (readFile) + +runDay :: Integer -> Integer -> IO () +runDay part1 part2 = + putStr "Part 1: " >> print part1 >> + putStr "Part 2: " >> print part2 withInput :: (String -> IO ()) -> IO () withInput f = do @@ -9,19 +16,14 @@ withInput f = do hSetBuffering stdout NoBuffering args <- getArgs - input <- - if null args - then readStdin - else readFile $ head args + input <- S.readFile $ head args f input -readStdin :: IO String -readStdin = do - eof <- isEOF - if eof - then pure "" - else do - line <- getLine - rest <- readStdin - pure $ line ++ rest +timeTaken :: IO () -> IO () +timeTaken f = do + start <- getCPUTime + f + end <- getCPUTime + let time = end - start + putStrLn $ " " ++ show (time `div` 1000000) ++ " µs" -- 2.11.4.GIT