[hs] Make cli fancier

This commit is contained in:
Joscha 2020-12-06 11:06:51 +00:00
parent 75fa851ff5
commit 55bc09948b
9 changed files with 56 additions and 36 deletions

View file

@ -4,18 +4,32 @@ import Control.Monad
import Options.Applicative import Options.Applicative
import Aoc.Day (Day (..)) import Aoc.Day (Day (..), Year (..))
import qualified Aoc.Y2019 as Y2019
import qualified Aoc.Y2020 as Y2020 import qualified Aoc.Y2020 as Y2020
toCommand :: Day -> Mod CommandFields (IO ()) lpad :: a -> Int -> [a] -> [a]
toCommand (DayPure name f) = command name $ info (helper <*> pure f) mempty lpad a n as = replicate (n - length as) a ++ as
toCommand (DayFile name f) = command name $ info (helper <*> p) mempty
where yearsToParser :: [Year] -> Parser (IO ())
p = f <$> strArgument (metavar "INPUTFILE") yearsToParser = hsubparser . mconcat . map yearToCommand
yearToCommand :: Year -> Mod CommandFields (IO ())
yearToCommand y = command (show $ yNum y) $ flip info mempty $ daysToParser $ yDays y
daysToParser :: [(Int, Day)] -> Parser (IO ())
daysToParser = hsubparser . mconcat . map (uncurry dayToCommand)
dayToCommand :: Int -> Day -> Mod CommandFields (IO ())
dayToCommand dNum = command (lpad '0' 2 $ show dNum) . flip info mempty . dayToParser
dayToParser :: Day -> Parser (IO ())
dayToParser (DayPure f) = pure f
dayToParser (DayFile f) = f <$> strArgument (metavar "INPUTFILE")
parser :: Parser (IO ()) parser :: Parser (IO ())
parser = subparser $ mconcat $ map toCommand $ Y2019.days ++ Y2020.days parser = yearsToParser
[ Y2020.year
]
opts :: ParserInfo (IO ()) opts :: ParserInfo (IO ())
opts = info (helper <*> parser) $ fullDesc <> failureCode 1 opts = info (helper <*> parser) $ fullDesc <> failureCode 1

View file

@ -1,5 +1,6 @@
module Aoc.Day module Aoc.Day
( Day(..) ( Year(..)
, Day(..)
, runDay , runDay
, dayPure , dayPure
, dayFile , dayFile
@ -16,29 +17,34 @@ import Text.Megaparsec
import Aoc.Parse import Aoc.Parse
data Year = Year
{ yNum :: Int
, yDays :: [(Int, Day)]
}
data Day data Day
= DayPure String (IO ()) = DayPure (IO ())
| DayFile String (FilePath -> IO ()) | DayFile (FilePath -> IO ())
-- | Helper function for trying out days in ghci. -- | Helper function for trying out days in ghci.
runDay :: Day -> FilePath -> IO () runDay :: Day -> FilePath -> IO ()
runDay (DayPure _ f) _ = f runDay (DayPure f) _ = f
runDay (DayFile _ f) p = f p runDay (DayFile f) p = f p
dayPure :: String -> IO () -> Day dayPure :: IO () -> Day
dayPure = DayPure dayPure = DayPure
dayFile :: String -> (FilePath -> IO ()) -> Day dayFile :: (FilePath -> IO ()) -> Day
dayFile = DayFile dayFile = DayFile
dayString :: String -> (String -> IO ()) -> Day dayString :: (String -> IO ()) -> Day
dayString name f = dayFile name $ f <=< readFile dayString f = dayFile $ f <=< readFile
dayText :: String -> (T.Text -> IO ()) -> Day dayText :: (T.Text -> IO ()) -> Day
dayText name f = dayFile name $ f <=< T.readFile dayText f = dayFile $ f <=< T.readFile
dayParse :: String -> Parser a -> (a -> IO ()) -> Day dayParse :: Parser a -> (a -> IO ()) -> Day
dayParse name p f = dayFile name $ \path -> do dayParse p f = dayFile $ \path -> do
text <- T.readFile path text <- T.readFile path
case parse (p <* eof) path text of case parse (p <* eof) path text of
Right a -> f a Right a -> f a

View file

@ -1,5 +1,5 @@
module Aoc.Y2020 module Aoc.Y2020
( days ( year
) where ) where
import Aoc.Day import Aoc.Day
@ -10,12 +10,12 @@ import qualified Aoc.Y2020.D04 as D04
import qualified Aoc.Y2020.D05 as D05 import qualified Aoc.Y2020.D05 as D05
import qualified Aoc.Y2020.D06 as D06 import qualified Aoc.Y2020.D06 as D06
days :: [Day] year :: Year
days = year = Year 2020
[ D01.day [ ( 1, D01.day)
, D02.day , ( 2, D02.day)
, D03.day , ( 3, D03.day)
, D04.day , ( 4, D04.day)
, D05.day , ( 5, D05.day)
, D06.day , ( 6, D06.day)
] ]

View file

@ -37,4 +37,4 @@ solver values = do
putStrLn $ show y1 ++ " * " ++ show y2 ++ " * " ++ show y3 ++ " = " ++ show (y1 * y2 * y3) putStrLn $ show y1 ++ " * " ++ show y2 ++ " * " ++ show y3 ++ " = " ++ show (y1 * y2 * y3)
day :: Day day :: Day
day = dayParse "2020_01" parser solver day = dayParse parser solver

View file

@ -42,4 +42,4 @@ solver ls = do
print $ length $ filter validPositions ls print $ length $ filter validPositions ls
day :: Day day :: Day
day = dayParse "2020_02" parser solver day = dayParse parser solver

View file

@ -44,4 +44,4 @@ solver trees = do
putStrLn $ "Product: " ++ show (oneOne * threeOne * fiveOne * sevenOne * oneTwo) putStrLn $ "Product: " ++ show (oneOne * threeOne * fiveOne * sevenOne * oneTwo)
day :: Day day :: Day
day = dayParse "2020_03" parser solver day = dayParse parser solver

View file

@ -157,4 +157,4 @@ solver passports = do
print $ length $ filter hasValidKeys passports print $ length $ filter hasValidKeys passports
day :: Day day :: Day
day = dayParse "2020_04" parser solver day = dayParse parser solver

View file

@ -40,4 +40,4 @@ solver ps = do
print $ mySeats $ Set.fromList ps print $ mySeats $ Set.fromList ps
day :: Day day :: Day
day = dayParse "2020_05" parser solver day = dayParse parser solver

View file

@ -22,4 +22,4 @@ solver groups = do
print $ sum $ map (Set.size . foldr1 Set.intersection . map Set.fromList) groups print $ sum $ map (Set.size . foldr1 Set.intersection . map Set.fromList) groups
day :: Day day :: Day
day = dayParse "2020_06" parser solver day = dayParse parser solver