-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay04.hs
46 lines (41 loc) · 1.18 KB
/
Day04.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
-- |
-- Module : AOC2022.Day04
-- License : BSD3
--
-- Stability : experimental
-- Portability : non-portable
--
-- Day 4. See "AOC.Solver" for the types used in this module!
module AOC2022.Day04 (
day04a,
day04b,
)
where
import AOC.Common (countTrue, listTup, listV2)
import AOC.Solver ((:~>) (..))
import Control.Monad ((<=<))
import qualified Data.ExtendedReal as E
import Data.IntegerInterval (IntegerInterval, (<=..<=))
import qualified Data.IntegerInterval as I
import Data.List.Split (splitOn)
import Linear.V2 (V2 (..))
import Text.Read (readMaybe)
parseIntervals :: String -> Maybe (V2 IntegerInterval)
parseIntervals =
traverse (fmap mkInterval . listTup <=< traverse readMaybe . splitOn "-")
<=< listV2 . splitOn ","
where
mkInterval (x, y) = E.Finite x <=..<= E.Finite y
day04 ::
(V2 IntegerInterval -> Bool) ->
[V2 IntegerInterval] :~> Int
day04 f =
MkSol
{ sParse = traverse parseIntervals . lines
, sShow = show
, sSolve = Just . countTrue f
}
day04a :: [V2 IntegerInterval] :~> Int
day04a = day04 \(V2 xs ys) -> xs `I.isSubsetOf` ys || ys `I.isSubsetOf` xs
day04b :: [V2 IntegerInterval] :~> Int
day04b = day04 \(V2 xs ys) -> xs I.==? ys