module Days.Day04 where

import AOCUtil

runA :: IO ()
runA = interactF "data/day04.txt" (show . solve . lines)

runB :: IO ()
runB = interactF "data/day04.txt" (show . solveB . lines)

type Section = (Int, Int)

type Pair = (Section, Section)

solve :: [String] -> Int
solve = length . filter sectionInclude . map readPair

readPair :: String -> Pair
readPair = first2 . map readSection . splitOn ','

readSection :: String -> Section
readSection = first2 . map read . splitOn '-'

sectionInclude :: Pair -> Bool
sectionInclude ((a, b), (c, d)) = (a <= c && d <= b) || (c <= a && b <= d)

solveB :: [String] -> Int
solveB = length . filter sectionOverlap . map readPair

sectionOverlap :: Pair -> Bool
sectionOverlap ((a, b), (c, d)) = not (b < c || d < a)
