From 1c66df880e818d8acceb1e954a844a3f6241ab3f Mon Sep 17 00:00:00 2001 From: Jack Kinsey Date: Sat, 17 Dec 2022 15:03:46 -0500 Subject: [PATCH] Day 4 Part 2 --- src/day04.fnl | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/day04.fnl b/src/day04.fnl index 5d42214..2488a1e 100644 --- a/src/day04.fnl +++ b/src/day04.fnl @@ -13,20 +13,31 @@ (or (and (>= r2l r1l) (<= r2h r1h)) (and (>= r1l r2l) (<= r1h r2h))))) +(defn range-overlaps [r1 r2] + (let [[r1l r1h] r1 + [r2l r2h] r2] + (or (and (>= r1h r2l) (<= r1l r2h)) + (and (>= r2h r1l) (<= r2l r1h))))) + (comment (parse-range "2-4") (parse-pair "2-4,6-8") (range-contains (unpack (parse-pair "2-8,3-7"))) (range-contains (unpack (parse-pair "5-7,7-9"))) (range-contains (unpack (parse-pair "6-6,4-6"))) + (range-overlaps (unpack (parse-pair "2-4,6-8"))) (part1 ["2-4,6-8" "2-3,4-5" "5-7,7-9" "2-8,3-7" "6-6,4-6" "2-6,4-8"]) - ) - + (part2 ["2-4,6-8" + "2-3,4-5" + "5-7,7-9" + "2-8,3-7" + "6-6,4-6" + "2-6,4-8"])) (defn part1 [lines] (->> lines @@ -35,7 +46,10 @@ (#))) (defn part2 [lines] - ) + (->> lines + (c.map parse-pair) + (c.filter #(range-overlaps (unpack $1))) + (#))) (comment (part1 (c.lines "input/day04.txt")) -- 2.38.5