Jens Kadenbach hace 1 año
padre
commit
1267be4d00
Se han modificado 10 ficheros con 1122 adiciones y 3 borrados
  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 Ver fichero

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

+ 1
- 0
package.yaml Ver fichero

@@ -29,6 +29,7 @@ dependencies:
29 29
 - containers
30 30
 - split
31 31
 - lens
32
+- Ranged-sets
32 33
 
33 34
 ghc-options:
34 35
 - -Wall

+ 1000
- 0
ressources/day04-input
La diferencia del archivo ha sido suprimido porque es demasiado grande
Ver fichero


+ 14
- 0
src/Day4.hs Ver fichero

@@ -0,0 +1,14 @@
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 Ver fichero

@@ -0,0 +1,16 @@
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 Ver fichero

@@ -0,0 +1,16 @@
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 Ver fichero

@@ -0,0 +1,29 @@
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 Ver fichero

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

+ 1
- 1
test/Day3Spec.hs Ver fichero

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

+ 35
- 0
test/Day4Spec.hs Ver fichero

@@ -0,0 +1,35 @@
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…
Cancelar
Guardar