From 91da51ec3e425f6cf66771af48cccdb98ea08a38 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Tue, 6 Dec 2022 12:03:40 +0000 Subject: [PATCH] Split datafile finding into a library --- advent-of-code22.cabal | 10 +++++++++- advent01/Main.hs | 12 +----------- advent02/Main.hs | 12 +----------- advent03/Main.hs | 12 +----------- advent04/Main-interval.hs | 12 +----------- advent04/Main.hs | 12 +----------- advent05/Main.hs | 14 +------------- advent06/Main.hs | 12 +----------- lib/AoC.hs | 13 +++++++++++++ 9 files changed, 29 insertions(+), 80 deletions(-) create mode 100644 lib/AoC.hs diff --git a/advent-of-code22.cabal b/advent-of-code22.cabal index f258fd8..747359d 100644 --- a/advent-of-code22.cabal +++ b/advent-of-code22.cabal @@ -73,6 +73,8 @@ common common-extensions common build-directives build-depends: base >=4.16 default-language: Haskell2010 + hs-source-dirs: ., app, lib + other-modules: AoC ghc-options: -O2 -Wall -threaded @@ -87,9 +89,15 @@ executable advent-of-code22 -- LANGUAGE extensions used by modules in this package. -- other-extensions: build-depends: base ^>=4.16.4.0 - hs-source-dirs: app + hs-source-dirs: app, lib default-language: Haskell2010 +library + import: common-extensions + build-depends: base >=4.16 + hs-source-dirs: ., app, lib + exposed-modules: AoC + executable advent01 import: common-extensions, build-directives main-is: advent01/Main.hs diff --git a/advent01/Main.hs b/advent01/Main.hs index 5a45a1e..cd13638 100644 --- a/advent01/Main.hs +++ b/advent01/Main.hs @@ -1,6 +1,6 @@ -- Writeup at https://work.njae.me.uk/2022/12/01/advent-of-code-2022-day-1/ -import System.Environment +import AoC import Data.List import Data.List.Split import Data.Ord @@ -13,16 +13,6 @@ main = print $ part1 calories print $ part2 calories -getDataFileName :: IO String -getDataFileName = - do args <- getArgs - progName <- getProgName - let baseDataName = if null args - then progName - else head args - let dataFileName = "data/" ++ baseDataName ++ ".txt" - return dataFileName - part1 :: [[Int]] -> Int part1 = maximum . fmap sum diff --git a/advent02/Main.hs b/advent02/Main.hs index 44a2361..2315317 100644 --- a/advent02/Main.hs +++ b/advent02/Main.hs @@ -1,6 +1,6 @@ -- Writeup at https://work.njae.me.uk/2022/12/02/advent-of-code-2022-day-2/ -import System.Environment +import AoC import Data.Text () import qualified Data.Text.IO as TIO import Data.Attoparsec.Text hiding (Result) @@ -20,16 +20,6 @@ main = let match2 = successfulParse2 text print $ part2 match2 -getDataFileName :: IO String -getDataFileName = - do args <- getArgs - progName <- getProgName - let baseDataName = if null args - then progName - else head args - let dataFileName = "data/" ++ baseDataName ++ ".txt" - return dataFileName - part1 :: [Round] -> Int part1 = sum . fmap scoreRound diff --git a/advent03/Main.hs b/advent03/Main.hs index 8ebba26..fa01d40 100644 --- a/advent03/Main.hs +++ b/advent03/Main.hs @@ -1,6 +1,6 @@ -- Writeup at https://work.njae.me.uk/2022/12/03/advent-of-code-2022-day-3/ -import System.Environment +import AoC import Data.Char import qualified Data.Set as S import Data.List @@ -17,16 +17,6 @@ main = print $ part1 rucksacks print $ part2 rucksacks -getDataFileName :: IO String -getDataFileName = - do args <- getArgs - progName <- getProgName - let baseDataName = if null args - then progName - else head args - let dataFileName = "data/" ++ baseDataName ++ ".txt" - return dataFileName - part1 :: [Rucksack] -> Int part1 = sum . fmap (priority . commonItem) diff --git a/advent04/Main-interval.hs b/advent04/Main-interval.hs index 7888fae..6ec57ba 100644 --- a/advent04/Main-interval.hs +++ b/advent04/Main-interval.hs @@ -1,6 +1,6 @@ -- Writeup at https://work.njae.me.uk/2022/12/04/advent-of-code-2022-day-4/ -import System.Environment +import AoC import Data.Text (Text) import qualified Data.Text.IO as TIO import Data.Attoparsec.Text hiding (Result) @@ -18,16 +18,6 @@ main = print $ part1 pairs print $ part2 pairs -getDataFileName :: IO String -getDataFileName = - do args <- getArgs - progName <- getProgName - let baseDataName = if null args - then progName - else head args - let dataFileName = "data/" ++ baseDataName ++ ".txt" - return dataFileName - part1 :: [Pair] -> Int part1 = length . (filter hasContainment) diff --git a/advent04/Main.hs b/advent04/Main.hs index a295abf..537d7c6 100644 --- a/advent04/Main.hs +++ b/advent04/Main.hs @@ -1,6 +1,6 @@ -- Writeup at https://work.njae.me.uk/2022/12/04/advent-of-code-2022-day-4/ -import System.Environment +import AoC import Data.Text (Text) import qualified Data.Text.IO as TIO import Data.Attoparsec.Text hiding (Result) @@ -17,16 +17,6 @@ main = print $ part1 pairs print $ part2 pairs -getDataFileName :: IO String -getDataFileName = - do args <- getArgs - progName <- getProgName - let baseDataName = if null args - then progName - else head args - let dataFileName = "data/" ++ baseDataName ++ ".txt" - return dataFileName - part1 :: [Pair] -> Int part1 = length . (filter hasContainment) diff --git a/advent05/Main.hs b/advent05/Main.hs index 21daf43..0b60f36 100644 --- a/advent05/Main.hs +++ b/advent05/Main.hs @@ -1,6 +1,6 @@ -- Writeup at https://work.njae.me.uk/2022/12/04/advent-of-code-2022-day-4/ -import System.Environment +import AoC import Data.Text (Text) import qualified Data.Text.IO as TIO import Data.Attoparsec.Text hiding (take) @@ -26,18 +26,6 @@ main = putStrLn $ part1 wharf moves putStrLn $ part2 wharf moves - -getDataFileName :: IO String -getDataFileName = - do args <- getArgs - progName <- getProgName - let baseDataName = if null args - then progName - else head args - let dataFileName = "data/" ++ baseDataName ++ ".txt" - return dataFileName - - part1 :: Wharf -> [Move] -> String part1 wharf moves = showTops $ applyMoves1 wharf moves diff --git a/advent06/Main.hs b/advent06/Main.hs index 02dd902..9852e8a 100644 --- a/advent06/Main.hs +++ b/advent06/Main.hs @@ -1,6 +1,6 @@ -- Writeup at https://work.njae.me.uk/2022/12/06/advent-of-code-2022-day-6/ -import System.Environment +import AoC import Data.List -- test = "mjqjpqmgbljsphdztnvjfqwrcgsmlb" @@ -14,16 +14,6 @@ main = print $ part1 text print $ part2 text -getDataFileName :: IO String -getDataFileName = - do args <- getArgs - progName <- getProgName - let baseDataName = if null args - then progName - else head args - let dataFileName = "data/" ++ baseDataName ++ ".txt" - return dataFileName - part1 :: String -> Int part1 = interestingPosition 4 diff --git a/lib/AoC.hs b/lib/AoC.hs new file mode 100644 index 0000000..764f2cc --- /dev/null +++ b/lib/AoC.hs @@ -0,0 +1,13 @@ +module AoC ( getDataFileName ) where + +import System.Environment + +getDataFileName :: IO String +getDataFileName = + do args <- getArgs + progName <- getProgName + let baseDataName = if null args + then progName + else head args + let dataFileName = "data/" ++ baseDataName ++ ".txt" + return dataFileName -- 2.34.1