Browse Source

day1

main
Jens Kadenbach 1 year ago
commit
c104561d8b
16 changed files with 2670 additions and 0 deletions
  1. 5
    0
      .gitignore
  2. 11
    0
      CHANGELOG.md
  3. 30
    0
      LICENSE
  4. 1
    0
      README.md
  5. 2
    0
      Setup.hs
  6. 75
    0
      aoc2022.cabal
  7. 6
    0
      app/Main.hs
  8. 63
    0
      package.yaml
  9. 2244
    0
      ressources/day01-input
  10. 14
    0
      ressources/day01-input-test
  11. 39
    0
      src/Day1.hs
  12. 66
    0
      src/Day1/Internal.hs
  13. 9
    0
      src/Lib.hs
  14. 67
    0
      stack.yaml
  15. 13
    0
      stack.yaml.lock
  16. 25
    0
      test/Spec.hs

+ 5
- 0
.gitignore View File

@@ -0,0 +1,5 @@
1
+.stack-work/
2
+*~
3
+.idea/
4
+aoc2022.iml
5
+out/

+ 11
- 0
CHANGELOG.md View File

@@ -0,0 +1,11 @@
1
+# Changelog for `aoc2022`
2
+
3
+All notable changes to this project will be documented in this file.
4
+
5
+The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+and this project adheres to the
7
+[Haskell Package Versioning Policy](https://pvp.haskell.org/).
8
+
9
+## Unreleased
10
+
11
+## 0.1.0.0 - YYYY-MM-DD

+ 30
- 0
LICENSE View File

@@ -0,0 +1,30 @@
1
+Copyright Jens Kadenbach (c) 2022
2
+
3
+All rights reserved.
4
+
5
+Redistribution and use in source and binary forms, with or without
6
+modification, are permitted provided that the following conditions are met:
7
+
8
+    * Redistributions of source code must retain the above copyright
9
+      notice, this list of conditions and the following disclaimer.
10
+
11
+    * Redistributions in binary form must reproduce the above
12
+      copyright notice, this list of conditions and the following
13
+      disclaimer in the documentation and/or other materials provided
14
+      with the distribution.
15
+
16
+    * Neither the name of Jens Kadenbach nor the names of other
17
+      contributors may be used to endorse or promote products derived
18
+      from this software without specific prior written permission.
19
+
20
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

+ 1
- 0
README.md View File

@@ -0,0 +1 @@
1
+# aoc2022

+ 2
- 0
Setup.hs View File

@@ -0,0 +1,2 @@
1
+import Distribution.Simple
2
+main = defaultMain

+ 75
- 0
aoc2022.cabal View File

@@ -0,0 +1,75 @@
1
+cabal-version: 1.12
2
+
3
+-- This file has been generated from package.yaml by hpack version 0.35.0.
4
+--
5
+-- see: https://github.com/sol/hpack
6
+
7
+name:           aoc2022
8
+version:        0.1.0.0
9
+description:    Please see the README on GitHub at <https://github.com/audax/aoc2022#readme>
10
+homepage:       https://github.com/audax/aoc2022#readme
11
+bug-reports:    https://github.com/audax/aoc2022/issues
12
+author:         Jens Kadenbach
13
+maintainer:     jens.kadenbach@smartsteuer.de
14
+copyright:      2022 Jens Kadenbach
15
+license:        BSD3
16
+license-file:   LICENSE
17
+build-type:     Simple
18
+extra-source-files:
19
+    README.md
20
+    CHANGELOG.md
21
+
22
+source-repository head
23
+  type: git
24
+  location: https://github.com/audax/aoc2022
25
+
26
+library
27
+  exposed-modules:
28
+      Day1
29
+      Day1.Internal
30
+      Lib
31
+  other-modules:
32
+      Paths_aoc2022
33
+  hs-source-dirs:
34
+      src
35
+  ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
36
+  build-depends:
37
+      QuickCheck
38
+    , base >=4.7 && <5
39
+    , hspec
40
+    , split
41
+    , text
42
+  default-language: Haskell2010
43
+
44
+executable aoc2022-exe
45
+  main-is: Main.hs
46
+  other-modules:
47
+      Paths_aoc2022
48
+  hs-source-dirs:
49
+      app
50
+  ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
51
+  build-depends:
52
+      QuickCheck
53
+    , aoc2022
54
+    , base >=4.7 && <5
55
+    , hspec
56
+    , split
57
+    , text
58
+  default-language: Haskell2010
59
+
60
+test-suite aoc2022-test
61
+  type: exitcode-stdio-1.0
62
+  main-is: Spec.hs
63
+  other-modules:
64
+      Paths_aoc2022
65
+  hs-source-dirs:
66
+      test
67
+  ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
68
+  build-depends:
69
+      QuickCheck
70
+    , aoc2022
71
+    , base >=4.7 && <5
72
+    , hspec
73
+    , split
74
+    , text
75
+  default-language: Haskell2010

+ 6
- 0
app/Main.hs View File

@@ -0,0 +1,6 @@
1
+module Main (main) where
2
+
3
+import Lib
4
+
5
+main :: IO ()
6
+main = someFunc

+ 63
- 0
package.yaml View File

@@ -0,0 +1,63 @@
1
+name:                aoc2022
2
+version:             0.1.0.0
3
+github:              "audax/aoc2022"
4
+license:             BSD3
5
+author:              "Jens Kadenbach"
6
+maintainer:          "jens.kadenbach@smartsteuer.de"
7
+copyright:           "2022 Jens Kadenbach"
8
+
9
+extra-source-files:
10
+- README.md
11
+- CHANGELOG.md
12
+
13
+# Metadata used when publishing your package
14
+# synopsis:            Short description of your package
15
+# category:            Web
16
+
17
+# To avoid duplicated efforts in documentation and dealing with the
18
+# complications of embedding Haddock markup inside cabal files, it is
19
+# common to point users to the README.md file.
20
+description:         Please see the README on GitHub at <https://github.com/audax/aoc2022#readme>
21
+
22
+dependencies:
23
+- base >= 4.7 && < 5
24
+- hspec
25
+- QuickCheck
26
+- text
27
+- split
28
+
29
+ghc-options:
30
+- -Wall
31
+- -Wcompat
32
+- -Widentities
33
+- -Wincomplete-record-updates
34
+- -Wincomplete-uni-patterns
35
+- -Wmissing-export-lists
36
+- -Wmissing-home-modules
37
+- -Wpartial-fields
38
+- -Wredundant-constraints
39
+
40
+library:
41
+  source-dirs: src
42
+
43
+executables:
44
+  aoc2022-exe:
45
+    main:                Main.hs
46
+    source-dirs:         app
47
+    ghc-options:
48
+    - -threaded
49
+    - -rtsopts
50
+    - -with-rtsopts=-N
51
+    dependencies:
52
+    - aoc2022
53
+
54
+tests:
55
+  aoc2022-test:
56
+    main:                Spec.hs
57
+    source-dirs:         test
58
+    ghc-options:
59
+    - -threaded
60
+    - -rtsopts
61
+    - -with-rtsopts=-N
62
+    dependencies:
63
+    - aoc2022

+ 2244
- 0
ressources/day01-input
File diff suppressed because it is too large
View File


+ 14
- 0
ressources/day01-input-test View File

@@ -0,0 +1,14 @@
1
+1000
2
+2000
3
+3000
4
+
5
+4000
6
+
7
+5000
8
+6000
9
+
10
+7000
11
+8000
12
+9000
13
+
14
+10000

+ 39
- 0
src/Day1.hs View File

@@ -0,0 +1,39 @@
1
+module Day1 (
2
+  day1,
3
+  findElfWithHighestCalorieCount,
4
+  findCaloriesOfTop3Elves
5
+) where
6
+
7
+import qualified Data.Text as T
8
+import Day1.Internal
9
+
10
+day1 :: IO ()
11
+day1 = do
12
+  putStrLn "Day1"
13
+  calorieCount <- day1_1
14
+  putStrLn ("Highest calorie count = " ++ calorieCount)
15
+  caloriesOfTop3Elves <- day1_2
16
+  putStrLn ("Calories of top 3 elves = " ++ caloriesOfTop3Elves)
17
+  putStrLn "-----------"
18
+
19
+
20
+day1_1 :: IO String
21
+day1_1 = do
22
+  input <- readFile "ressources/day01-input"
23
+  return (show $ findElfWithHighestCalorieCount $ T.pack input)
24
+
25
+day1_2 :: IO String
26
+day1_2 = do
27
+  input <- readFile "ressources/day01-input"
28
+  return (show $ findCaloriesOfTop3Elves $ T.pack input)
29
+
30
+
31
+findCaloriesOfTop3Elves :: T.Text -> CalorieCount
32
+findCaloriesOfTop3Elves  =
33
+  calories . sumCalories . take 3 . sortElves . elfToCalories
34
+  where
35
+    sumCalories :: [Elf] -> Int
36
+    sumCalories = sum . map caloriesOf
37
+
38
+findElfWithHighestCalorieCount :: T.Text -> Elf
39
+findElfWithHighestCalorieCount = head .sortElves . elfToCalories

+ 66
- 0
src/Day1/Internal.hs View File

@@ -0,0 +1,66 @@
1
+{-# LANGUAGE OverloadedStrings #-}
2
+module Day1.Internal (
3
+  elfToCalories,
4
+  Elf,
5
+  CalorieCount,
6
+  calories,
7
+  elf,
8
+  parseElfGroups,
9
+  intOrZero,
10
+  sortElves,
11
+  caloriesOf
12
+) where
13
+
14
+import qualified Data.Text as T
15
+import qualified Data.Text.Read as R
16
+import Data.List.Split
17
+import qualified Data.List as L
18
+import Data.Either
19
+
20
+newtype CalorieCount = CalorieCount Int
21
+newtype Elf = Elf CalorieCount
22
+
23
+instance Show CalorieCount where
24
+  show (CalorieCount c) = "Calories[" ++ show c ++ "]"
25
+
26
+instance Show Elf where
27
+  show (Elf (CalorieCount c)) = "Elf[" ++ show c ++ "]"
28
+  
29
+instance Eq CalorieCount where
30
+  (==) (CalorieCount c1) (CalorieCount c2)  = c1 == c2
31
+
32
+instance Eq Elf where
33
+  (==) (Elf (CalorieCount c1)) (Elf (CalorieCount c2))  = c1 == c2
34
+
35
+instance Ord Elf where
36
+  compare (Elf (CalorieCount c1)) (Elf (CalorieCount c2)) = compare c1 c2
37
+
38
+elf :: Int -> Elf
39
+elf c = Elf (CalorieCount c)
40
+
41
+calories :: Int -> CalorieCount
42
+calories = CalorieCount
43
+
44
+caloriesOf :: Elf -> Int
45
+caloriesOf (Elf (CalorieCount c)) = c
46
+
47
+sortElves :: [Elf] -> [Elf]
48
+sortElves = L.reverse . L.sort
49
+
50
+elfToCalories :: T.Text -> [Elf]
51
+elfToCalories input = map elf sums
52
+  where
53
+    groups = parseElfGroups input
54
+    sums = map sum groups
55
+
56
+parseElfGroups :: T.Text -> [[Int]]
57
+parseElfGroups input = map (map intOrZero) groups
58
+  where
59
+  groups = splitWhen (== "") $ T.lines input
60
+
61
+
62
+intOrZero:: T.Text -> Int
63
+intOrZero text = val
64
+  where
65
+  (val, _) = fromRight (0, "") $ R.decimal text
66
+

+ 9
- 0
src/Lib.hs View File

@@ -0,0 +1,9 @@
1
+module Lib
2
+    ( someFunc
3
+    ) where
4
+      
5
+import Day1      
6
+
7
+someFunc :: IO ()
8
+someFunc = day1
9
+

+ 67
- 0
stack.yaml View File

@@ -0,0 +1,67 @@
1
+# This file was automatically generated by 'stack init'
2
+#
3
+# Some commonly used options have been documented as comments in this file.
4
+# For advanced use and comprehensive documentation of the format, please see:
5
+# https://docs.haskellstack.org/en/stable/yaml_configuration/
6
+
7
+# Resolver to choose a 'specific' stackage snapshot or a compiler version.
8
+# A snapshot resolver dictates the compiler version and the set of packages
9
+# to be used for project dependencies. For example:
10
+#
11
+# resolver: lts-3.5
12
+# resolver: nightly-2015-09-21
13
+# resolver: ghc-7.10.2
14
+#
15
+# The location of a snapshot can be provided as a file or url. Stack assumes
16
+# a snapshot provided as a file might change, whereas a url resource does not.
17
+#
18
+# resolver: ./custom-snapshot.yaml
19
+# resolver: https://example.com/snapshots/2018-01-01.yaml
20
+resolver:
21
+  url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/2.yaml
22
+
23
+# User packages to be built.
24
+# Various formats can be used as shown in the example below.
25
+#
26
+# packages:
27
+# - some-directory
28
+# - https://example.com/foo/bar/baz-0.0.2.tar.gz
29
+#   subdirs:
30
+#   - auto-update
31
+#   - wai
32
+packages:
33
+- .
34
+# Dependency packages to be pulled from upstream that are not in the resolver.
35
+# These entries can reference officially published versions as well as
36
+# forks / in-progress versions pinned to a git hash. For example:
37
+#
38
+# extra-deps:
39
+# - acme-missiles-0.3
40
+# - git: https://github.com/commercialhaskell/stack.git
41
+#   commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
42
+#
43
+# extra-deps: []
44
+
45
+# Override default flag values for local packages and extra-deps
46
+# flags: {}
47
+
48
+# Extra package databases containing global packages
49
+# extra-package-dbs: []
50
+
51
+# Control whether we use the GHC we find on the path
52
+# system-ghc: true
53
+#
54
+# Require a specific version of stack, using version ranges
55
+# require-stack-version: -any # Default
56
+# require-stack-version: ">=2.9"
57
+#
58
+# Override the architecture used by stack, especially useful on Windows
59
+# arch: i386
60
+# arch: x86_64
61
+#
62
+# Extra directories used by stack for building
63
+# extra-include-dirs: [/path/to/dir]
64
+# extra-lib-dirs: [/path/to/dir]
65
+#
66
+# Allow a newer minor version of GHC than the snapshot specifies
67
+# compiler-check: newer-minor

+ 13
- 0
stack.yaml.lock View File

@@ -0,0 +1,13 @@
1
+# This file was autogenerated by Stack.
2
+# You should not edit this file by hand.
3
+# For more information, please see the documentation at:
4
+#   https://docs.haskellstack.org/en/stable/lock_files
5
+
6
+packages: []
7
+snapshots:
8
+- completed:
9
+    sha256: fc39d8afc97531d53d87b10abdef593bce503c0c1e46c2e9a84ebcbc78bf8470
10
+    size: 648432
11
+    url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/2.yaml
12
+  original:
13
+    url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/2.yaml

+ 25
- 0
test/Spec.hs View File

@@ -0,0 +1,25 @@
1
+{-# LANGUAGE OverloadedStrings #-}
2
+import Test.Hspec
3
+import Day1
4
+import Day1.Internal
5
+import qualified Data.Text as T
6
+
7
+main :: IO ()
8
+main = hspec $ do
9
+  describe "Day1" $ do
10
+    it "sorts elves" $ do
11
+       let input = [ elf 6000, elf 4000, elf 11000]
12
+       sortElves input `shouldBe` [ elf 11000, elf 6000, elf 4000]
13
+    it "converts a list of strings to a list of either int or caloriecount" $ do
14
+       input <- readFile "ressources/day01-input-test"
15
+       elfToCalories (T.pack input) `shouldBe`
16
+          [ elf 6000, elf 4000, elf 11000, elf 24000, elf 10000 ]
17
+    it "parser text to int" $ do
18
+        intOrZero "1000" `shouldBe` 1000
19
+    it "finds the elf with the highest calorie count" $ do
20
+       input <- readFile "ressources/day01-input-test"
21
+       findElfWithHighestCalorieCount (T.pack input) `shouldBe` elf 24000
22
+    it "sums the calorie counts" $ do
23
+       input <- readFile "ressources/day01-input-test"
24
+       findCaloriesOfTop3Elves (T.pack input) `shouldBe` calories 45000
25
+

Loading…
Cancel
Save