Files
AoC2022/day4.hs
Amneesh Singh 38201248c4 day4,5: update - use parser combinators
not the cleanest, im still learning parsing in hs

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-12-05 23:39:03 +05:30

35 lines
739 B
Haskell

import Data.Char (isDigit)
import Text.ParserCombinators.ReadP
( ReadP,
char,
many1,
readP_to_S,
satisfy,
)
main :: IO ()
main = do
input <- map (fst . last . readP_to_S parse) . lines <$> readFile "day4.in"
putStr "Q1: "
print $ q1 input
putStr "Q2: "
print $ q2 input
parse :: ReadP ((Int, Int), (Int, Int))
parse = do
a <- readInt
char '-'
b <- readInt
char ','
c <- readInt
char '-'
d <- readInt
return ((a, b), (c, d))
where
readInt :: ReadP Int
readInt = read <$> many1 (satisfy isDigit)
q1, q2 :: [((Int, Int), (Int, Int))] -> Int
q1 = length . filter (\((a, b), (c, d)) -> a >= c && b <= d || c >= a && d <= b)
q2 = length . filter (\((a, b), (c, d)) -> b >= c && a <= d)