diff --git a/solutions/Days/Day04.hs b/solutions/Days/Day04.hs index fcb50a7..df7032c 100644 --- a/solutions/Days/Day04.hs +++ b/solutions/Days/Day04.hs @@ -1,16 +1,30 @@ +{-# LANGUAGE OverloadedStrings #-} + module Days.Day04 (day04) where import AOC (Solution (..)) -import qualified Data.Text as T +import Data.Text qualified as T day04 :: Solution day04 = Solution parseInput part1 part2 -parseInput :: T.Text -> a -parseInput = error "parseInput not defined for day 04" +parseInput :: T.Text -> [[[Int]]] +parseInput = map (map (map (read . T.unpack) . T.splitOn "-") . T.splitOn ",") . T.lines -part1 :: a -> Int -part1 = error "part1 not defined for day 04" +part1 :: [[[Int]]] -> Int +part1 input = sum $ map logic input + where + logic [[u1, u2], [d1, d2]] + | u1 >= d1 && u2 <= d2 = 1 + | d1 >= u1 && d2 <= u2 = 1 + | otherwise = 0 -part2 :: a -> Int -part2 = error "part2 not defined for day 04" +part2 :: [[[Int]]] -> Int +part2 input = sum $ map logic input + where + logic [[u1, u2], [d1, d2]] + | u1 >= d1 && u1 <= d2 = 1 + | u2 >= d1 && u2 <= d2 = 1 + | d1 >= u1 && d1 <= u2 = 1 + | d2 >= u1 && d2 <= u2 = 1 + | otherwise = 0