File tree 7 files changed +71
-1
lines changed
7 files changed +71
-1
lines changed Original file line number Diff line number Diff line change @@ -8,3 +8,4 @@ Development occurs in language-specific directories:
8
8
| [ Day1.hs] ( hs/src/Day1.hs ) | [ Day1.kt] ( kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day1.kt ) | [ day1.py] ( py/aoc2022/day1.py ) | [ day1.rs] ( rs/src/day1.rs ) |
9
9
| [ Day2.hs] ( hs/src/Day2.hs ) | [ Day2.kt] ( kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day2.kt ) | [ day2.py] ( py/aoc2022/day2.py ) | [ day2.rs] ( rs/src/day2.rs ) |
10
10
| [ Day3.hs] ( hs/src/Day3.hs ) | [ Day3.kt] ( kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day3.kt ) | [ day3.py] ( py/aoc2022/day3.py ) | [ day3.rs] ( rs/src/day3.rs ) |
11
+ | [ Day4.hs] ( hs/src/Day4.hs ) |
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ data-files:
14
14
day1.txt
15
15
, day2.txt
16
16
, day3.txt
17
+ , day4.txt
17
18
18
19
extra-source-files :
19
20
README.md
@@ -23,6 +24,7 @@ library
23
24
Day1
24
25
, Day2
25
26
, Day3
27
+ , Day4
26
28
build-depends :
27
29
base ^>= 4.16.0.0
28
30
, split ^>= 0.2.3.5
@@ -57,6 +59,7 @@ test-suite aoc2022-test
57
59
Day1Spec
58
60
, Day2Spec
59
61
, Day3Spec
62
+ , Day4Spec
60
63
hs-source-dirs : test
61
64
default-language : GHC2021
62
65
build-tool-depends :
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ module Main (main) where
4
4
import Day1 (day1a , day1b )
5
5
import Day2 (day2a , day2b )
6
6
import Day3 (day3a , day3b )
7
+ import Day4 (day4a , day4b )
7
8
8
9
import Control.Monad ((<=<) , when )
9
10
import Data.Maybe (mapMaybe )
@@ -30,3 +31,4 @@ main = do
30
31
run 1 print [day1a, day1b]
31
32
run 2 print [day2a, day2b]
32
33
run 3 print [day3a, day3b]
34
+ run 4 (either fail print ) [day4a, day4b]
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import qualified Data.Text.IO as TIO (readFile)
6
6
import Day1 (day1a , day1b )
7
7
import Day2 (day2a , day2b )
8
8
import Day3 (day3a , day3b )
9
+ import Day4 (day4a , day4b )
9
10
import Paths_aoc2022 (getDataFileName )
10
11
11
12
getDayInput :: Int -> IO Text
@@ -25,4 +26,8 @@ main = defaultMain
25
26
[ bench " part 1" $ nf day3a input
26
27
, bench " part 2" $ nf day3b input
27
28
]
29
+ , env (getDayInput 4 ) $ \ input -> bgroup " Day 4"
30
+ [ bench " part 1" $ nf day4a input
31
+ , bench " part 2" $ nf day4b input
32
+ ]
28
33
]
Original file line number Diff line number Diff line change 1
- module Common (readEntire ) where
1
+ module Common (count , readEntire ) where
2
2
3
+ import Data.Foldable (toList )
3
4
import Data.Text (Text )
4
5
import qualified Data.Text as T (null )
5
6
import Data.Text.Read (Reader )
6
7
8
+ count :: (Foldable t ) => (a -> Bool ) -> t a -> Int
9
+ count p = length . filter p . toList
10
+
7
11
readEntire :: Reader a -> Text -> Either String a
8
12
readEntire reader input = do
9
13
(a, t) <- reader input
Original file line number Diff line number Diff line change
1
+ {-|
2
+ Module: Day4
3
+ Description: <https://adventofcode.com/2022/day/4 Day 4: Camp Cleanup>
4
+ -}
5
+ {-# LANGUAGE OverloadedStrings #-}
6
+ module Day4 (day4a , day4b ) where
7
+
8
+ import Common (count )
9
+ import Control.Monad (ap )
10
+ import Data.Maybe (fromMaybe )
11
+ import Data.Text (Text )
12
+ import qualified Data.Text as T (lines , stripPrefix )
13
+ import qualified Data.Text.Read as T (decimal )
14
+
15
+ parse :: (Integral a ) => Text -> Either String (a , a , a , a )
16
+ parse line = do
17
+ (a, line) <- T. decimal line
18
+ (b, line) <- T. decimal $ fromMaybe `ap` T. stripPrefix " -" $ line
19
+ (c, line) <- T. decimal $ fromMaybe `ap` T. stripPrefix " ," $ line
20
+ (d, line) <- T. decimal $ fromMaybe `ap` T. stripPrefix " -" $ line
21
+ pure (a, b, c, d)
22
+
23
+ day4a :: Text -> Either String Int
24
+ day4a input = fmap (count ok) . sequence $ parse <$> T. lines input where
25
+ ok (a, b, c, d) = a >= c && b <= d || a <= c && b >= d
26
+
27
+ day4b :: Text -> Either String Int
28
+ day4b input = fmap (count ok) . sequence $ parse <$> T. lines input where
29
+ ok (a, b, c, d) = a <= d && b >= c
Original file line number Diff line number Diff line change
1
+ {-# LANGUAGE OverloadedStrings #-}
2
+ module Day4Spec (spec ) where
3
+
4
+ import Data.Text (Text )
5
+ import qualified Data.Text as T (unlines )
6
+ import Day4 (day4a , day4b )
7
+ import Test.Hspec (Spec , describe , it , shouldBe )
8
+
9
+ example :: Text
10
+ example = T. unlines
11
+ [ " 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
+ spec :: Spec
20
+ spec = do
21
+ describe " part 1" $ do
22
+ it " examples" $ do
23
+ day4a example `shouldBe` Right 2
24
+ describe " part 2" $ do
25
+ it " examples" $ do
26
+ day4b example `shouldBe` Right 4
You can’t perform that action at this time.
0 commit comments