[hs] Add day module

To help with reading input etc. for a single day
This commit is contained in:
Joscha 2020-12-03 00:08:48 +00:00
parent 17379cc287
commit 3b0ea44036
2 changed files with 42 additions and 0 deletions

View file

@ -4,6 +4,7 @@ version: 0.1.0.0
dependencies:
- base >= 4.7 && < 5
- containers
- megaparsec
- optparse-applicative
- text

41
hs/src/Aoc/Day.hs Normal file
View file

@ -0,0 +1,41 @@
module Aoc.Day
( Day(..)
, dayPure
, dayFile
, dayString
, dayText
, Parser
, dayParser
) where
import Control.Monad
import Data.Void
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Text.Megaparsec
data Day
= DayPure String (IO ())
| DayFile String (FilePath -> IO ())
dayPure :: String -> IO () -> Day
dayPure = DayPure
dayFile :: String -> (FilePath -> IO ()) -> Day
dayFile = DayFile
dayString :: String -> (String -> IO ()) -> Day
dayString name f = dayFile name $ f <=< readFile
dayText :: String -> (T.Text -> IO ()) -> Day
dayText name f = dayFile name $ f <=< T.readFile
type Parser = Parsec Void T.Text
dayParser :: String -> Parser a -> (a -> IO ()) -> Day
dayParser name p f = dayFile name $ \path -> do
text <- T.readFile path
case parse p path text of
Right a -> f a
Left e -> putStrLn $ errorBundlePretty e