From 7c0680ca49a153d524aeea5e802f0dca8331ab75 Mon Sep 17 00:00:00 2001 From: sgf Date: Thu, 27 Jan 2022 23:03:03 +0300 Subject: [PATCH] chg(haskell): Organize code in the same way as Go code. --- Main.hs | 10 ++++++++++ aoc2021.cabal | 26 +++++++++++++------------- day1/{Main.hs => Day1.hs} | 26 ++++++++++++++++++++------ day6/{Main.hs => Day6.hs} | 8 +++++--- 4 files changed, 48 insertions(+), 22 deletions(-) create mode 100644 Main.hs rename day1/{Main.hs => Day1.hs} (56%) rename day6/{Main.hs => Day6.hs} (99%) diff --git a/Main.hs b/Main.hs new file mode 100644 index 0000000..eff1986 --- /dev/null +++ b/Main.hs @@ -0,0 +1,10 @@ + +--import qualified Day1 as Day1 +import qualified Day6 as Day6 + +main :: IO () +main = do + --runF1 "day1/input.txt" + --runF2 "day1/input.txt" + + Day6.runF1 "day6/input.txt" diff --git a/aoc2021.cabal b/aoc2021.cabal index b4a9088..16294a3 100644 --- a/aoc2021.cabal +++ b/aoc2021.cabal @@ -20,26 +20,26 @@ source-repository head type: git location: https://github.com/githubuser/aoc2021 -executable day1 - main-is: Main.hs - other-modules: - Paths_aoc2021 +library hs-source-dirs: - day1 - ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall + day1 + day6 + ghc-options: -Wall build-depends: - base >=4.7 && <5 - default-language: Haskell2010 + base >=4.7 && <5, + comonad, + split + exposed-modules: + Day1 + Day6 -executable day6 +executable aoc2021 main-is: Main.hs other-modules: Paths_aoc2021 - hs-source-dirs: - day6 ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall build-depends: base >=4.7 && <5, - comonad, - split + aoc2021 default-language: Haskell2010 + diff --git a/day1/Main.hs b/day1/Day1.hs similarity index 56% rename from day1/Main.hs rename to day1/Day1.hs index d3eac3d..df51605 100644 --- a/day1/Main.hs +++ b/day1/Day1.hs @@ -1,3 +1,10 @@ + +module Day1 + ( runF1 + , runF2 + ) + where + f1 :: [Int] -> Int f1 [] = 0 -- y x(cur) @@ -17,9 +24,16 @@ f2 (k1 : k2 : ks) = fst $ foldr go (\_ _ -> (0, 0)) ks k1 k2 cur = w + y + x in if next > cur then (n + 1, cur) else (n, cur) -main :: IO () -main = do - c <- readFile "day1/input.txt" - let xs = map read (lines c) - print $ "Answer1: " ++ show (f1 xs) - print $ "Answer2: " ++ show (f2 xs) +readInput :: FilePath -> IO [Int] +readInput = fmap (map read . lines) . readFile + +runF1 :: FilePath -> IO () +runF1 fp = do + xs <- readInput fp + print $ "Answer1: " ++ show (f1 xs) + +runF2 :: FilePath -> IO () +runF2 fp = do + xs <- readInput fp + print $ "Answer2: " ++ show (f2 xs) + diff --git a/day6/Main.hs b/day6/Day6.hs similarity index 99% rename from day6/Main.hs rename to day6/Day6.hs index 1160614..a073bad 100644 --- a/day6/Main.hs +++ b/day6/Day6.hs @@ -1,4 +1,9 @@ +module Day6 + ( runF1 + ) + where + import Control.Comonad import Control.Comonad.Traced import Data.Function @@ -154,6 +159,3 @@ runF1 fp = do let ys = fishesF ages 80 print $ "Answer1: " ++ show (length ys) -main :: IO () -main = runF1 "day6/input.txt" - -- 2.20.1