Browse Source

Use stack as build tool

master
Nicolas Lenz 6 months ago
parent
commit
ebb315dea7
7 changed files with 101 additions and 39 deletions
  1. 0
    39
      01.hs
  2. 37
    0
      advent-of-code.cabal
  3. 0
    0
      day01.input
  4. 27
    0
      package.yaml
  5. 23
    0
      src/Day01.hs
  6. 10
    0
      src/Main.hs
  7. 4
    0
      stack.yaml

+ 0
- 39
01.hs View File

@@ -1,39 +0,0 @@
1
-import System.Environment
2
-
3
-main :: IO ()
4
-main = do
5
-    args <- getArgs
6
-    case args of
7
-         ["part1"] -> part1
8
-         ["part2"] -> part2
9
-         _ -> putStrLn "gief part"
10
-
11
-part1 :: IO ()
12
-part1 = do
13
-    freqChanges <- readFreqChanges
14
-    print $ last $ applySeq freqChanges 0
15
-
16
-part2 :: IO ()
17
-part2 = do
18
-    freqChanges <- readFreqChanges
19
-    print $ firstTwice $ applySeq (cycle freqChanges) 0
20
-
21
-readFreqChanges :: IO [Integer -> Integer]
22
-readFreqChanges = do
23
-    input <- readFile "01.input"
24
-    return (toFunction <$> lines input)
25
-
26
-toFunction :: String -> (Integer -> Integer)
27
-toFunction ('+':as) = (+(read as))
28
-toFunction ('-':as) = (\x -> x - read as)
29
-
30
-applySeq :: [a -> a] -> a -> [a]
31
-applySeq [] start = [start]
32
-applySeq (f:fs) start = start : applySeq fs (f start)
33
-
34
-firstTwice :: Eq a => [a] -> Maybe a
35
-firstTwice as = loop as [] where
36
-    loop [] found = Nothing
37
-    loop todo@(x:xs) found
38
-        | x `elem` found = Just x
39
-        | otherwise = loop xs (x:found)

+ 37
- 0
advent-of-code.cabal View File

@@ -0,0 +1,37 @@
1
+cabal-version: 1.12
2
+
3
+-- This file has been generated from package.yaml by hpack version 0.31.1.
4
+--
5
+-- see: https://github.com/sol/hpack
6
+--
7
+-- hash: ec04a239f02e42ba3551e0a7529c4268de82830c0633716ed9ffd4c2ff152247
8
+
9
+name:           advent-of-code
10
+version:        0.0.0
11
+synopsis:       My solutions for the Advent of Code
12
+description:    Please see the README at <https://git.eisfunke.com/research/advent-of-code#readme>
13
+category:       Puzzle
14
+author:         Nicolas Lenz
15
+maintainer:     nicolas@eisfunke.com
16
+copyright:      2018 Nicolas Lenz
17
+license:        MIT
18
+license-file:   LICENSE
19
+build-type:     Simple
20
+extra-source-files:
21
+    README.md
22
+
23
+source-repository head
24
+  type: git
25
+  location: https://git.eisfunke.com/research/advent-of-code
26
+
27
+executable advent-of-code
28
+  main-is: Main.hs
29
+  other-modules:
30
+      Day01
31
+      Paths_advent_of_code
32
+  hs-source-dirs:
33
+      src
34
+  ghc-options: -O2 -threaded -rtsopts -with-rtsopts=-N
35
+  build-depends:
36
+      base >=4.7 && <5
37
+  default-language: Haskell2010

01.input → day01.input View File


+ 27
- 0
package.yaml View File

@@ -0,0 +1,27 @@
1
+name:                advent-of-code
2
+version:             0.0.0
3
+license:             MIT
4
+git:                 "https://git.eisfunke.com/research/advent-of-code"
5
+author:              "Nicolas Lenz"
6
+maintainer:          "nicolas@eisfunke.com"
7
+copyright:           "2018 Nicolas Lenz"
8
+
9
+extra-source-files:
10
+- README.md
11
+
12
+synopsis:            My solutions for the Advent of Code
13
+category:            Puzzle
14
+description:         Please see the README at <https://git.eisfunke.com/research/advent-of-code#readme>
15
+
16
+dependencies:
17
+- base >= 4.7 && < 5
18
+
19
+executables:
20
+  advent-of-code:
21
+    main:                Main.hs
22
+    source-dirs:         src
23
+    ghc-options:
24
+    - -O2
25
+    - -threaded
26
+    - -rtsopts
27
+    - -with-rtsopts=-N

+ 23
- 0
src/Day01.hs View File

@@ -0,0 +1,23 @@
1
+module Day01 (part1, part2) where
2
+
3
+part1 :: [String] -> IO ()
4
+part1 input = print $ last $ applySeq (toFunction <$> input) 0
5
+
6
+part2 :: [String] -> IO ()
7
+part2 input = print $ firstTwice $ applySeq (cycle (toFunction <$> input)) 0
8
+
9
+
10
+toFunction :: String -> (Integer -> Integer)
11
+toFunction ('+':as) = (+(read as))
12
+toFunction ('-':as) = (\x -> x - read as)
13
+
14
+applySeq :: [a -> a] -> a -> [a]
15
+applySeq [] start = [start]
16
+applySeq (f:fs) start = start : applySeq fs (f start)
17
+
18
+firstTwice :: Eq a => [a] -> Maybe a
19
+firstTwice as = loop as [] where
20
+    loop [] found = Nothing
21
+    loop todo@(x:xs) found
22
+        | x `elem` found = Just x
23
+        | otherwise = loop xs (x:found)

+ 10
- 0
src/Main.hs View File

@@ -0,0 +1,10 @@
1
+import System.Environment
2
+import Day01
3
+
4
+main :: IO ()
5
+main = do
6
+    args <- getArgs
7
+    case args of
8
+         ["01-1"] -> readFile "day01.input" >>= Day01.part1 . lines
9
+         ["01-2"] -> readFile "day01.input" >>= Day01.part1 . lines
10
+         _ -> putStrLn "Example call: advent-of-code 01-1 for day 01, part 1\nInput should be named like day01.input."

+ 4
- 0
stack.yaml View File

@@ -0,0 +1,4 @@
1
+resolver: lts-12.19
2
+
3
+packages:
4
+- .

Loading…
Cancel
Save