diff --git a/04/Main.idr b/04/Main.idr index 543b0aa..585a235 100644 --- a/04/Main.idr +++ b/04/Main.idr @@ -20,6 +20,10 @@ parseRange str = contains : Range -> Range -> Bool contains outer inner = (start outer <= start inner) && (end outer >= end inner) +-- Returns true if the first range contains part of the second +overlaps : Range -> Range -> Bool +overlaps outer inner = start outer <= start inner && end outer >= start inner + record RangePair where constructor MkRangePair first, second : Range @@ -38,6 +42,10 @@ parseRangePair str = doesContain : RangePair -> Bool doesContain pair = contains (first pair) (second pair) || contains (second pair) (first pair) +-- Returns true if one of the ranges overlaps the other +doesOverlap : RangePair -> Bool +doesOverlap pair = overlaps (first pair) (second pair) || overlaps (second pair) (first pair) + main : IO () main = @@ -46,6 +54,9 @@ main = Right content => let pairs = catMaybes . map parseRangePair . lines $ content containsCount = length . filter doesContain $ pairs + overlapsCount = length . filter doesOverlap $ pairs in do putStr "Part 1: " printLn containsCount + putStr "Part 2: " + printLn overlapsCount Left err => printLn err