chg(haskell): Organize code in the same way as Go code.
authorsgf <sgf.dma@gmail.com>
Thu, 27 Jan 2022 20:03:03 +0000 (23:03 +0300)
committersgf <sgf.dma@gmail.com>
Thu, 27 Jan 2022 20:07:36 +0000 (23:07 +0300)
Main.hs [new file with mode: 0644]
aoc2021.cabal
day1/Day1.hs [moved from day1/Main.hs with 56% similarity]
day6/Day6.hs [moved from day6/Main.hs with 99% similarity]

diff --git a/Main.hs b/Main.hs
new file mode 100644 (file)
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"
index b4a9088..16294a3 100644 (file)
@@ -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
+
similarity index 56%
rename from day1/Main.hs
rename to day1/Day1.hs
index d3eac3d..df51605 100644 (file)
@@ -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)
+
similarity index 99%
rename from day6/Main.hs
rename to day6/Day6.hs
index 1160614..a073bad 100644 (file)
@@ -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"
-