Overengineered Day4, part two

This commit is contained in:
Martin Potier 2022-12-16 21:07:17 +02:00
parent e5a0801e06
commit 7852714f36
No known key found for this signature in database
GPG key ID: D4DD957DBA4AD89E
2 changed files with 16 additions and 1 deletions

View file

@ -31,3 +31,5 @@ main = do
let demoInput4 = "2-4,6-8\n2-3,4-5\n5-7,7-9\n2-8,3-7\n6-6,4-6\n2-6,4-8" let demoInput4 = "2-4,6-8\n2-3,4-5\n5-7,7-9\n2-8,3-7\n6-6,4-6\n2-6,4-8"
putStrLn $ show $ Day4.solveA demoInput4 putStrLn $ show $ Day4.solveA demoInput4
putStrLn $ show $ Day4.solveA input4 putStrLn $ show $ Day4.solveA input4
putStrLn $ show $ Day4.solveB demoInput4
putStrLn $ show $ Day4.solveB input4

View file

@ -3,9 +3,9 @@
module AoC.Day4 where module AoC.Day4 where
import Data.Text (Text) import Data.Text (Text)
-- import qualified Data.Text as T
import Data.Attoparsec.Text import Data.Attoparsec.Text
import Data.Bits (xor)
type Lower = Int type Lower = Int
type Higher = Int type Higher = Int
@ -36,3 +36,16 @@ solveA :: Text -> Either String Int
solveA txt = do solveA txt = do
lines' <- parseInput txt lines' <- parseInput txt
pure $ (length . filter (uncurry eitherIncluded)) lines' pure $ (length . filter (uncurry eitherIncluded)) lines'
--------------------------------------------------------------------------------
-- Part 2
overlap :: Range1D -> Range1D -> Bool
overlap (Range1D l1 r1) (Range1D l2 r2) = (r1 >= l2) && (l1 <= r2)
eitherOverlap :: Range1D -> Range1D -> Bool
eitherOverlap range1 range2 = range1 `overlap` range2
solveB :: Text -> Either String Int
solveB txt = do
lines' <- parseInput txt
pure $ (length . filter (uncurry eitherOverlap)) lines'