-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.hs
52 lines (41 loc) · 1.07 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
47
48
49
50
51
52
#!/usr/bin/env cabal
{- cabal:
build-depends: base
, trifecta
-}
import System.IO (readFile)
import Text.Trifecta
import Data.Foldable (foldr)
import Control.Monad (mfilter)
type Range = (Integer, Integer)
type Pair = (Range, Range)
range :: Parser Range
range = do
a <- integer
char '-'
b <- integer
return (a, b)
pair :: Parser Pair
pair = do
a <- range
char ','
b <- range
return (a, b)
pairs :: Parser [Pair]
pairs = some pair
contains :: Range -> Range -> Bool
contains (a0, a1) (b0, b1) = a0 <= b0 && b1 <= a1
overlaps :: Range -> Range -> Bool
overlaps (a0, a1) (b0, b1) = (a0 <= b0 && b0 <= a1) || (b0 <= a0 && a0 <= b1)
problem1 :: [Pair] -> Integer
problem1 pairs =
foldr (\_ acc -> acc + 1) 0 (mfilter (\(a, b) -> contains a b || contains b a) pairs)
problem2 :: [Pair] -> Integer
problem2 pairs =
foldr (\_ acc -> acc + 1) 0 (mfilter (\(a, b) -> overlaps a b) pairs)
main :: IO ()
main = do
input <- readFile "day04.txt"
let thePairs = parseString pairs mempty input
print $ problem1 <$> thePairs
print $ problem2 <$> thePairs