-
Notifications
You must be signed in to change notification settings - Fork 4
/
Day4.hs
29 lines (25 loc) · 1 KB
/
Day4.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
{-|
Module: Day4
Description: <https://adventofcode.com/2022/day/4 Day 4: Camp Cleanup>
-}
{-# LANGUAGE OverloadedStrings #-}
module Day4 (day4a, day4b) where
import Common (count)
import Control.Monad (ap)
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import qualified Data.Text as T (lines, stripPrefix)
import qualified Data.Text.Read as T (decimal)
parse :: (Integral a) => Text -> Either String (a, a, a, a)
parse line = do
(a, line) <- T.decimal line
(b, line) <- T.decimal $ fromMaybe `ap` T.stripPrefix "-" $ line
(c, line) <- T.decimal $ fromMaybe `ap` T.stripPrefix "," $ line
(d, line) <- T.decimal $ fromMaybe `ap` T.stripPrefix "-" $ line
pure (a, b, c, d)
day4a :: Text -> Either String Int
day4a input = fmap (count ok) . sequence $ parse <$> T.lines input where
ok (a, b, c, d) = a >= c && b <= d || a <= c && b >= d
day4b :: Text -> Either String Int
day4b input = fmap (count ok) . sequence $ parse <$> T.lines input where
ok (a, b, c, d) = a <= d && b >= c