AoC 2022 D4: Camp Cleanup

Haskell | Problem statement | Source code | Tags: Data structures

← Previous Back to AoC Index Next →

Part 1

Let the two ranges be (a1,b1)(a_1, b_1) and (a2,b2)(a_2, b_2). One range fully contains the other if and only if a1a2a_1 \leq a_2 and b1b2b_1 \geq b_2, or a2a1a_2 \leq a_1 and b2b1b_2 \geq b_1.

contain :: [(Int, Int)] -> Bool
contain [(a1, b1), (a2, b2)] = (a1 <= a2 && b1 >= b2) || (a1 >= a2 && b1 <= b2)
hs

GHC.Utils.Misc has a useful function count, so the answer is just count contain lines.

Part 2

Two ranges (a1,b1)(a_1, b_1) and (a2,b2)(a_2, b_2) overlap if and only if they do not satisfy the condition b1<a2b_1 < a_2 (range 1 is entirely less than 2) or b2<a1b_2 < a_1 (range 2 is entirely less than 1). In other words, we need b1a2b_1 \geq a_2 and b2a1b_2 \geq a_1.

overlap [(a1, b1), (a2, b2)] = b1 >= a2 && b2 >= a1
hs

← Previous Back to AoC Index Next →