From 0b36acbe058a0afb63f2c35ce8cc24fca042e8f3 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Tue, 7 Feb 2023 18:48:59 +0100 Subject: day04 --- AoC2022/aocclj/src/aocclj/day04.clj | 23 +++++++++++++++++++++++ AoC2022/aocclj/test/aocclj/core_test.clj | 7 +++++++ 2 files changed, 30 insertions(+) create mode 100644 AoC2022/aocclj/src/aocclj/day04.clj diff --git a/AoC2022/aocclj/src/aocclj/day04.clj b/AoC2022/aocclj/src/aocclj/day04.clj new file mode 100644 index 0000000..5b03eeb --- /dev/null +++ b/AoC2022/aocclj/src/aocclj/day04.clj @@ -0,0 +1,23 @@ +(ns aocclj.day04 + (:require [clojure.string :as str])) + +(defn subinterval [a0 a1 b0 b1] + (and (<= a0 b0) (>= a1 b1))) + +(defn subcontained [a0 a1 b0 b1] + (or (subinterval a0 a1 b0 b1) + (subinterval b0 b1 a0 a1))) + +(defn overlap [a0 a1 b0 b1] + "Test if [b0;b1] overlaps [a0;a1]" + (and (<= a0 b1) (<= b0 a1))) + +(defn solver [func input] + (transduce + (comp + (map #(str/split % #"[,-]")) + (map (partial map parse-long)) + (map #(apply func %)) + (map #(if (true? %) 1 0))) + + + input)) diff --git a/AoC2022/aocclj/test/aocclj/core_test.clj b/AoC2022/aocclj/test/aocclj/core_test.clj index dc3f3bb..fd34fe0 100644 --- a/AoC2022/aocclj/test/aocclj/core_test.clj +++ b/AoC2022/aocclj/test/aocclj/core_test.clj @@ -2,6 +2,7 @@ (:require [clojure.test :refer :all] [aocclj.day01 :as day01] [aocclj.day03 :as day03] + [aocclj.day04 :as day04] [aocclj.core :refer :all] [clojure.string :as str])) @@ -16,3 +17,9 @@ (are [expected function] (= expected (function input)) 8072 day03/part1 2567 day03/part2))) + +(deftest day04 + (let [input (str/split-lines (slurp "../04/input"))] + (are [expected function] (= expected (day04/solver function input)) + 515 day04/subcontained + 883 day04/overlap))) -- cgit v1.2.3