From 3f082a7eafae2aee82afbfa6cde64a4a02b95629 Mon Sep 17 00:00:00 2001 From: Jack Kinsey Date: Sun, 11 Dec 2022 19:40:08 -0500 Subject: [PATCH] Day 4 Part 1 --- src/common.fnl | 2 ++ src/day00.fnl | 15 +++++++++++++++ src/day04.fnl | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 src/day00.fnl create mode 100644 src/day04.fnl diff --git a/src/common.fnl b/src/common.fnl index c2974c0..e39868c 100644 --- a/src/common.fnl +++ b/src/common.fnl @@ -16,6 +16,8 @@ (defn map [f tbl] (icollect [_ v (ipairs tbl)] (f v))) +(defn filter [f tbl] (icollect [_ v (ipairs tbl)] (if (f v) v nil))) + (defn add [a b] (+ a b)) (defn gt [a b] (> a b)) diff --git a/src/day00.fnl b/src/day00.fnl new file mode 100644 index 0000000..79e9c19 --- /dev/null +++ b/src/day00.fnl @@ -0,0 +1,15 @@ +(module net.journcy.aoc2022.day00 + {autoload {c net.journcy.aoc2022.common}}) + +(comment +) + +(defn part1 [lines] + ) + +(defn part2 [lines] + ) + +(comment + (part1 (c.lines "input/day00.txt")) + (part2 (c.lines "input/day00.txt"))) diff --git a/src/day04.fnl b/src/day04.fnl new file mode 100644 index 0000000..5d42214 --- /dev/null +++ b/src/day04.fnl @@ -0,0 +1,42 @@ +(module net.journcy.aoc2022.day04 + {autoload {c net.journcy.aoc2022.common}}) + +(defn parse-range [rangestr] + (c.map tonumber (c.split rangestr "-"))) + +(defn parse-pair [pairstr] + (c.map parse-range (c.split pairstr ","))) + +(defn range-contains [r1 r2] + (let [[r1l r1h] r1 + [r2l r2h] r2] + (or (and (>= r2l r1l) (<= r2h r1h)) + (and (>= r1l r2l) (<= r1h r2h))))) + +(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"))) + (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"]) + ) + + +(defn part1 [lines] + (->> lines + (c.map parse-pair) + (c.filter #(range-contains (unpack $1))) + (#))) + +(defn part2 [lines] + ) + +(comment + (part1 (c.lines "input/day04.txt")) + (part2 (c.lines "input/day04.txt"))) -- 2.38.5