Day 4 Part 2

This commit is contained in:
Nathan McCarty 2022-12-04 11:53:40 -05:00
parent d53d873554
commit 463df72095
Signed by: thatonelutenist
GPG Key ID: D70DA3DD4D1E9F96
1 changed files with 11 additions and 0 deletions

View File

@ -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