From c0c9e1c623afab71e7fd9a9f51cfb358fcaab221 Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 5 Dec 2019 08:54:33 +0000 Subject: [PATCH] Re-add first solution from temporary aoc repo --- hs/app/Main.hs | 26 ++++++++++++++++++++++++-- hs/package.yaml | 1 + hs/src/Aoc/Y2019/A01.hs | 19 +++++++++++++++++++ hs/src/Lib.hs | 6 ------ 4 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 hs/src/Aoc/Y2019/A01.hs delete mode 100644 hs/src/Lib.hs diff --git a/hs/app/Main.hs b/hs/app/Main.hs index de1c1ab..586ff71 100644 --- a/hs/app/Main.hs +++ b/hs/app/Main.hs @@ -1,6 +1,28 @@ module Main where -import Lib +import Options.Applicative + +import Aoc.Y2019.A01 + +data Settings = Settings + { function :: FilePath -> IO () + , filename :: FilePath + } + +solutions :: Parser (FilePath -> IO ()) +solutions = subparser $ mconcat $ map (\(name, func) -> command name (info (pure func) mempty)) + [ ("201901", solve201901) + ] + +parser :: Parser Settings +parser = Settings + <$> solutions + <*> strArgument (metavar "INPUTFILE") + +opts :: ParserInfo Settings +opts = info (helper <*> parser) $ fullDesc <> failureCode 1 main :: IO () -main = someFunc +main = do + settings <- execParser opts + function settings $ filename settings diff --git a/hs/package.yaml b/hs/package.yaml index e47ee0a..b83a802 100644 --- a/hs/package.yaml +++ b/hs/package.yaml @@ -21,6 +21,7 @@ description: Please see the README on GitHub at = 4.7 && < 5 +- optparse-applicative library: source-dirs: src diff --git a/hs/src/Aoc/Y2019/A01.hs b/hs/src/Aoc/Y2019/A01.hs new file mode 100644 index 0000000..f68440a --- /dev/null +++ b/hs/src/Aoc/Y2019/A01.hs @@ -0,0 +1,19 @@ +module Aoc.Y2019.A01 + ( solve201901 + ) where + +fuel :: Integer -> Integer +fuel n = (n `div` 3) - 2 + +iteratedFuel :: Integer -> Integer +iteratedFuel = sum . takeWhile (> 0) . tail . iterate fuel + +solve201901 :: FilePath -> IO () +solve201901 f = do + values <- (map read . lines) <$> readFile f + + putStr "Total fuel: " + print $ sum $ map fuel values + + putStr "Total fuel (iterated): " + print $ sum $ map iteratedFuel values diff --git a/hs/src/Lib.hs b/hs/src/Lib.hs deleted file mode 100644 index d36ff27..0000000 --- a/hs/src/Lib.hs +++ /dev/null @@ -1,6 +0,0 @@ -module Lib - ( someFunc - ) where - -someFunc :: IO () -someFunc = putStrLn "someFunc"