2022-12-06 16:09:51 -05:00

46 lines
1.5 KiB
Haskell

module Day4Lib
( day4,
day4Alternate
) where
import Data.List (intersect, isSubsequenceOf)
import Data.List.Split (splitOn)
type SectionId = Int
type Range = (SectionId, SectionId)
type RangePair = (Range, Range)
checkRangePairForPartialOverlap :: RangePair -> Bool
checkRangePairForPartialOverlap (r1, r2) = intersection
where
processedR1 = processRange r1
processedR2 = processRange r2
-- Get the intersection, convert it into a bool based on if it's empty, then negate the bool
intersection = not $ null $ processedR1 `intersect` processedR2
checkRangePairForFullOverlap :: RangePair -> Bool
checkRangePairForFullOverlap (r1, r2) = processedR1 `isSubsequenceOf` processedR2 || processedR2 `isSubsequenceOf` processedR1
where
processedR1 = processRange r1
processedR2 = processRange r2
processRange :: Range -> [SectionId]
processRange (s, e) = [s..e]
tuplify2 :: [a] -> (a,a)
tuplify2 [x,y] = (x, y)
parseInput :: String -> [RangePair]
parseInput i = map tuplify2 $ map (map tuplify2) $ map (map (map readAsInt)) $ map (map splitOnDash) $ map splitOnComma $ lines i
where
inputAsLines = lines i
splitOnComma = splitOn ","
splitOnDash = splitOn "-"
readAsInt r = read r :: Int
day4 :: String -> Int
day4 input = sum $ map fromEnum $ map checkRangePairForFullOverlap $ parseInput input
day4Alternate :: String -> Int
day4Alternate input = sum $ map fromEnum $ map checkRangePairForPartialOverlap $ parseInput input