Browse Source

Day 4

main
Jens Kadenbach 1 year ago
parent
commit
1267be4d00
10 changed files with 1122 additions and 3 deletions
  1. 8
    0
      aoc2022.cabal
  2. 1
    0
      package.yaml
  3. 1000
    0
      ressources/day04-input
  4. 14
    0
      src/Day4.hs
  5. 16
    0
      src/Day4/Part1.hs
  6. 16
    0
      src/Day4/Part2.hs
  7. 29
    0
      src/Day4/Shared.hs
  8. 2
    2
      src/Lib.hs
  9. 1
    1
      test/Day3Spec.hs
  10. 35
    0
      test/Day4Spec.hs

+ 8
- 0
aoc2022.cabal View File

35
       Day3.Part1
35
       Day3.Part1
36
       Day3.Part2
36
       Day3.Part2
37
       Day3.Shared
37
       Day3.Shared
38
+      Day4
39
+      Day4.Part1
40
+      Day4.Part2
41
+      Day4.Shared
38
       Lib
42
       Lib
39
   other-modules:
43
   other-modules:
40
       Paths_aoc2022
44
       Paths_aoc2022
43
   ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
47
   ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
44
   build-depends:
48
   build-depends:
45
       QuickCheck
49
       QuickCheck
50
+    , Ranged-sets
46
     , base >=4.7 && <5
51
     , base >=4.7 && <5
47
     , containers
52
     , containers
48
     , heredoc
53
     , heredoc
61
   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
66
   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
62
   build-depends:
67
   build-depends:
63
       QuickCheck
68
       QuickCheck
69
+    , Ranged-sets
64
     , aoc2022
70
     , aoc2022
65
     , base >=4.7 && <5
71
     , base >=4.7 && <5
66
     , containers
72
     , containers
78
       Day1Spec
84
       Day1Spec
79
       Day2Spec
85
       Day2Spec
80
       Day3Spec
86
       Day3Spec
87
+      Day4Spec
81
       Paths_aoc2022
88
       Paths_aoc2022
82
   hs-source-dirs:
89
   hs-source-dirs:
83
       test
90
       test
84
   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
91
   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
85
   build-depends:
92
   build-depends:
86
       QuickCheck
93
       QuickCheck
94
+    , Ranged-sets
87
     , aoc2022
95
     , aoc2022
88
     , base >=4.7 && <5
96
     , base >=4.7 && <5
89
     , containers
97
     , containers

+ 1
- 0
package.yaml View File

29
 - containers
29
 - containers
30
 - split
30
 - split
31
 - lens
31
 - lens
32
+- Ranged-sets
32
 
33
 
33
 ghc-options:
34
 ghc-options:
34
 - -Wall
35
 - -Wall

+ 1000
- 0
ressources/day04-input
File diff suppressed because it is too large
View File


+ 14
- 0
src/Day4.hs View File

1
+module Day4 (day4) where
2
+
3
+import Day4.Part1
4
+import Day4.Part2
5
+
6
+day4 :: IO ()
7
+day4 = do
8
+   input <- readFile "ressources/day04-input"
9
+   putStrLn "Day4"
10
+   let score1 = countFullyOverlappingPairs input
11
+   putStrLn ("Number of fully overlapping section pairs " ++ show score1)
12
+   let score2 = countOverlappingPairs input
13
+   putStrLn ("Number of section pairs that overlap at all " ++ show score2)
14
+   return ()

+ 16
- 0
src/Day4/Part1.hs View File

1
+module Day4.Part1 (
2
+  isFullyOverlapping,
3
+  countFullyOverlappingPairs
4
+)
5
+ where
6
+   
7
+import Day4.Shared
8
+import Data.Ranged
9
+
10
+isFullyOverlapping :: ElfPair -> Bool
11
+isFullyOverlapping (a, b) = a `rangeEncloses` b || b `rangeEncloses` a
12
+
13
+countFullyOverlappingPairs :: String -> Int
14
+countFullyOverlappingPairs input = length $ filter isFullyOverlapping pairs
15
+  where
16
+    pairs = map elfPair $ lines input

+ 16
- 0
src/Day4/Part2.hs View File

1
+module Day4.Part2 (
2
+  countOverlappingPairs
3
+) where
4
+
5
+import Day4.Shared
6
+import Data.Ranged
7
+
8
+
9
+isOverlapping :: ElfPair -> Bool
10
+isOverlapping (a, b) = a `rangeOverlap` b || b `rangeOverlap` a
11
+
12
+countOverlappingPairs :: String -> Int
13
+countOverlappingPairs input = length $ filter isOverlapping pairs
14
+  where
15
+    pairs = map elfPair $ lines input
16
+

+ 29
- 0
src/Day4/Shared.hs View File

1
+module Day4.Shared(
2
+  section,
3
+  elfPair,
4
+  (+=+),
5
+  Section,
6
+  ElfPair
7
+) where
8
+
9
+
10
+import Data.Ranged
11
+import Control.Lens
12
+
13
+type Section = Range Int
14
+type ElfPair = (Section, Section)
15
+
16
+(+=+) :: Int -> Int -> Range Int
17
+x +=+ y = Range (BoundaryBelow x) (BoundaryAbove y)
18
+
19
+section :: String -> Section
20
+section s = read start +=+ read end
21
+  where
22
+    (start, rest) = break ('-' ==) s
23
+    end = drop 1 rest
24
+
25
+
26
+elfPair :: String -> ElfPair
27
+elfPair s = over each section (first, drop 1 second)
28
+  where
29
+    (first, second) = break (',' ==) s

+ 2
- 2
src/Lib.hs View File

2
     ( someFunc
2
     ( someFunc
3
     ) where
3
     ) where
4
       
4
       
5
-import Day3      
5
+import Day4      
6
 
6
 
7
 someFunc :: IO ()
7
 someFunc :: IO ()
8
-someFunc = day3
8
+someFunc = day4
9
 
9
 

+ 1
- 1
test/Day3Spec.hs View File

26
 
26
 
27
 spec :: Spec
27
 spec :: Spec
28
 spec =
28
 spec =
29
-  describe "Day2" $ do
29
+  describe "Day3" $ do
30
     describe "Part1" $ do
30
     describe "Part1" $ do
31
       it "can determine Rucksack compartment contents" $ do
31
       it "can determine Rucksack compartment contents" $ do
32
         splitContents simpleContents `shouldBe` ("abc", "dec")
32
         splitContents simpleContents `shouldBe` ("abc", "dec")

+ 35
- 0
test/Day4Spec.hs View File

1
+{-# LANGUAGE QuasiQuotes #-}
2
+module Day4Spec (spec) where
3
+
4
+import Test.Hspec
5
+import Text.Heredoc
6
+import Day4.Part1
7
+import Day4.Part2
8
+import Day4.Shared
9
+
10
+inputPart1 :: String
11
+inputPart1 = [str|2-4,6-8
12
+                 |2-3,4-5
13
+                 |5-7,7-9
14
+                 |2-8,3-7
15
+                 |6-6,4-6
16
+                 |2-6,4-8
17
+                 |]
18
+
19
+
20
+spec :: Spec
21
+spec =
22
+  describe "Day4" $ do
23
+    describe "Part 1" $ do
24
+      it "parses section" $ do
25
+        section "2-4" `shouldBe` 2 +=+ 4
26
+      it "parses section pair" $ do
27
+        elfPair "2-4,6-8" ` shouldBe` (2 +=+ 4, 6 +=+ 8)
28
+      it "finds fully overlapping sections" $ do
29
+        isFullyOverlapping (3 +=+ 6, 4 +=+ 6) `shouldBe` True
30
+      it "counts fully overlapping pairs" $ do
31
+        countFullyOverlappingPairs inputPart1 `shouldBe` 2
32
+    describe "Part 2" $ do
33
+      it "counts overlapping pairs" $ do
34
+        countOverlappingPairs inputPart1 `shouldBe` 4
35
+

Loading…
Cancel
Save