Reorganize imports
This commit is contained in:
36
Y2022.cabal
36
Y2022.cabal
@@ -12,31 +12,45 @@ source-repository head
|
||||
location: https://github.com/ctsk/aoc-2022
|
||||
|
||||
common warnings
|
||||
ghc-options: -Wall -O2 -ddump-simpl -ddump-to-file
|
||||
ghc-options: -Wall
|
||||
|
||||
common defaults
|
||||
default-language: Haskell2010
|
||||
build-depends:
|
||||
base ^>=4.18.0.0
|
||||
, extra ^>=1.7.14
|
||||
, containers
|
||||
|
||||
|
||||
library
|
||||
default-language: Haskell2010
|
||||
import: warnings, defaults
|
||||
hs-source-dirs: src
|
||||
ghc-options: -Wall -O2 -ddump-simpl -ddump-to-file
|
||||
exposed-modules:
|
||||
Lib
|
||||
Print
|
||||
other-modules:
|
||||
Common
|
||||
Parse
|
||||
Print
|
||||
Util
|
||||
Days.D01
|
||||
Days.D02
|
||||
Days.D03
|
||||
build-depends:
|
||||
base ^>=4.18.0.0
|
||||
, extra ^>=1.7.14
|
||||
, megaparsec ^>=9.4.0
|
||||
megaparsec ^>=9.4.0
|
||||
, text
|
||||
|
||||
executable Y2022
|
||||
import: warnings
|
||||
import: warnings, defaults
|
||||
hs-source-dirs: app
|
||||
main-is: Main.hs
|
||||
build-depends:
|
||||
Y2022
|
||||
, base ^>=4.18.0.0
|
||||
hs-source-dirs: app
|
||||
default-language: Haskell2010
|
||||
|
||||
benchmark Y2022-bench
|
||||
import: defaults
|
||||
type: exitcode-stdio-1.0
|
||||
hs-source-dirs: bench
|
||||
main-is: Bench.hs
|
||||
build-depends:
|
||||
Y2022
|
||||
, criterion ^>=1.6.3.0
|
||||
18
app/Main.hs
18
app/Main.hs
@@ -3,21 +3,19 @@
|
||||
module Main where
|
||||
|
||||
import Lib
|
||||
import Print
|
||||
|
||||
import qualified Days.D01 as D01
|
||||
import qualified Days.D02 as D02
|
||||
import qualified Days.D03 as D03
|
||||
|
||||
import System.Environment (getArgs)
|
||||
|
||||
solutions :: [(Int, Day, FilePath)]
|
||||
solutions =
|
||||
[ (1, D01.day, "./data/01.in")
|
||||
, (2, D02.day, "./data/02.in")
|
||||
, (3, D03.day, "./data/03.in")
|
||||
paths :: [FilePath]
|
||||
paths =
|
||||
[ "./data/01.in"
|
||||
, "./data/02.in"
|
||||
, "./data/03.in"
|
||||
]
|
||||
|
||||
solutions :: [(Int, Day, FilePath)]
|
||||
solutions = zip3 [1 ..] (map head days) paths
|
||||
|
||||
runAll :: [(Int, Day, FilePath)] -> IO ()
|
||||
runAll = mapM_ (\(dayNum, day, path) -> run day path >>= printDR dayNum)
|
||||
|
||||
|
||||
9
bench/Bench.hs
Normal file
9
bench/Bench.hs
Normal file
@@ -0,0 +1,9 @@
|
||||
module Main where
|
||||
|
||||
import Criterion (bench, whnfIO)
|
||||
import Criterion.Main (defaultMain)
|
||||
|
||||
main =
|
||||
defaultMain
|
||||
[ bench "whnfIO readFile" $ whnfIO (readFile "Y2022.cabal")
|
||||
]
|
||||
28
src/Common.hs
Normal file
28
src/Common.hs
Normal file
@@ -0,0 +1,28 @@
|
||||
{-# LANGUAGE GADTs #-}
|
||||
{-# LANGUAGE ImportQualifiedPost #-}
|
||||
|
||||
module Common where
|
||||
|
||||
import Data.Text qualified as T
|
||||
|
||||
data Answer where
|
||||
Answer :: (Show a) => a -> Answer
|
||||
|
||||
instance Show Answer where
|
||||
showsPrec p (Answer a) = showsPrec p a
|
||||
|
||||
data PuzzleError
|
||||
= MalformedInput String
|
||||
| Unsolvable
|
||||
|
||||
type Result = Maybe Answer
|
||||
|
||||
definitive :: (Show a) => a -> Result
|
||||
definitive = Just . Answer
|
||||
|
||||
type DayResult = Either String (Result, Result)
|
||||
|
||||
data Day
|
||||
= TextDay (T.Text -> DayResult)
|
||||
| StringDay (String -> DayResult)
|
||||
| ParsecDay (FilePath -> T.Text -> DayResult)
|
||||
@@ -1,9 +1,8 @@
|
||||
module Days.D01 where
|
||||
|
||||
import Lib (Day, Parser, definitive, parsecDay)
|
||||
import Parse (number, someLines)
|
||||
|
||||
import Common
|
||||
import Data.List (sortBy)
|
||||
import Parse
|
||||
|
||||
type Intermediate = [[Int]]
|
||||
|
||||
|
||||
@@ -3,10 +3,9 @@
|
||||
|
||||
module Days.D02 where
|
||||
|
||||
import Lib
|
||||
import Parse
|
||||
|
||||
import Common
|
||||
import Data.Functor ((<&>))
|
||||
import Parse
|
||||
import Text.Megaparsec (oneOf)
|
||||
import Text.Megaparsec.Char (space)
|
||||
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
{-# LANGUAGE ImportQualifiedPost #-}
|
||||
|
||||
module Days.D03 where
|
||||
|
||||
import Common
|
||||
|
||||
import Data.Char (ord)
|
||||
import Data.List (intersect)
|
||||
import Data.List.Extra (chunksOf)
|
||||
import Lib (Day (StringDay), definitive)
|
||||
|
||||
type Intermediate = [String]
|
||||
|
||||
|
||||
62
src/Lib.hs
62
src/Lib.hs
@@ -1,55 +1,29 @@
|
||||
{-# LANGUAGE ExistentialQuantification #-}
|
||||
{-# LANGUAGE GADTs #-}
|
||||
{-# LANGUAGE ImportQualifiedPost #-}
|
||||
|
||||
module Lib where
|
||||
module Lib (
|
||||
run,
|
||||
Day,
|
||||
days,
|
||||
printDR,
|
||||
) where
|
||||
|
||||
import Util (both)
|
||||
import Common (Day (..), DayResult)
|
||||
import Print (printDR)
|
||||
|
||||
import Days.D01 qualified as D01
|
||||
import Days.D02 qualified as D02
|
||||
import Days.D03 qualified as D03
|
||||
|
||||
import Data.Function ((&))
|
||||
import Data.Text qualified as T
|
||||
import Data.Text.IO qualified as T
|
||||
import Data.Void (Void)
|
||||
import Text.Megaparsec qualified as M (Parsec, eof, errorBundlePretty, parse)
|
||||
|
||||
data Answer where
|
||||
Answer :: (Show a) => a -> Answer
|
||||
|
||||
instance Show Answer where
|
||||
showsPrec p (Answer a) = showsPrec p a
|
||||
|
||||
data PuzzleError
|
||||
= MalformedInput String
|
||||
| Unsolvable
|
||||
|
||||
type Result = Maybe Answer
|
||||
|
||||
definitive :: (Show a) => a -> Result
|
||||
definitive = Just . Answer
|
||||
|
||||
type DayResult = Either String (Result, Result)
|
||||
|
||||
data Day
|
||||
= TextDay (T.Text -> DayResult)
|
||||
| StringDay (String -> DayResult)
|
||||
| ParsecDay (FilePath -> T.Text -> DayResult)
|
||||
|
||||
run :: Day -> FilePath -> IO DayResult
|
||||
run (TextDay f) = fmap f . T.readFile
|
||||
run (StringDay f) = fmap f . readFile
|
||||
run (ParsecDay f) = \path -> f path <$> T.readFile path
|
||||
|
||||
type Parser = M.Parsec Void T.Text
|
||||
|
||||
parsecDay ::
|
||||
Parser a ->
|
||||
(a -> Result, a -> Result) ->
|
||||
Day
|
||||
parsecDay parser parts =
|
||||
ParsecDay
|
||||
( \path text ->
|
||||
( case M.parse (parser <* M.eof) path text of
|
||||
Left e -> Left $ M.errorBundlePretty e
|
||||
Right parsedInput -> Right $ both (parsedInput &) parts
|
||||
)
|
||||
)
|
||||
days :: [[Day]]
|
||||
days =
|
||||
[ [D01.day]
|
||||
, [D02.day]
|
||||
, [D03.day]
|
||||
]
|
||||
|
||||
22
src/Parse.hs
22
src/Parse.hs
@@ -2,14 +2,32 @@
|
||||
|
||||
module Parse where
|
||||
|
||||
import Lib (Parser)
|
||||
|
||||
import Common
|
||||
import Data.Function ((&))
|
||||
import Data.Text qualified as T
|
||||
import Data.Void
|
||||
import Text.Megaparsec qualified as M
|
||||
import Text.Megaparsec.Char qualified as MC
|
||||
import Text.Megaparsec.Char.Lexer qualified as MCL
|
||||
import Util (both)
|
||||
|
||||
someLines :: Parser a -> Parser [a]
|
||||
someLines p = p `M.sepEndBy1` MC.newline
|
||||
|
||||
number :: Parser Int
|
||||
number = MCL.decimal
|
||||
|
||||
type Parser = M.Parsec Void T.Text
|
||||
|
||||
parsecDay ::
|
||||
Parser a ->
|
||||
(a -> Result, a -> Result) ->
|
||||
Day
|
||||
parsecDay parser parts =
|
||||
ParsecDay
|
||||
( \path text ->
|
||||
( case M.parse (parser <* M.eof) path text of
|
||||
Left e -> Left $ M.errorBundlePretty e
|
||||
Right parsedInput -> Right $ both (parsedInput &) parts
|
||||
)
|
||||
)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
module Print where
|
||||
|
||||
import Lib (DayResult)
|
||||
import Common
|
||||
import Text.Printf (printf)
|
||||
|
||||
printDR :: Int -> DayResult -> IO ()
|
||||
|
||||
Reference in New Issue
Block a user