diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2022-12-04 11:38:51 +0200 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2022-12-04 11:38:51 +0200 |
| commit | 576d3eae2d75a05b8191d7168e19ff71a8c3b0e0 (patch) | |
| tree | 11b4e482da22cf27b2d3bc41cc890d8527e744d6 /src/aoc_2022 | |
| parent | 07f4901c7cfc929ed7ee7e559e6735a11bc9c7e8 (diff) | |
Solve day04
Diffstat (limited to 'src/aoc_2022')
| -rw-r--r-- | src/aoc_2022/day04.clj | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/aoc_2022/day04.clj b/src/aoc_2022/day04.clj new file mode 100644 index 0000000..a6ca91b --- /dev/null +++ b/src/aoc_2022/day04.clj @@ -0,0 +1,38 @@ +(ns aoc-2022.day04) +(require '[clojure.java.io :as io]) +(require '[aoc-2022.utils :as utils]) +(require '[clojure.string :as str]) + +(defn parse-rows [rows] + (->> rows + (map #(str/split % #",")) + (map (fn [[a, b]] + [(map #(Long/valueOf %) (str/split a #"-")), + (map #(Long/valueOf %) (str/split b #"-"))])) + (map flatten))) + +(defn assignment-contains [[a1 a2 b1 b2]] + (or + (and (>= a1 b1) (<= a2 b2)) + (and (>= b1 a1) (<= b2 a2)))) + +(defn task1 [rows] + (->> rows + parse-rows + (filter assignment-contains) + count)) + +(defn assignment-overlaps [[a1 a2 b1 b2]] + (not (or (> a1 b2) (> b1 a2)))) + +(defn task2 [rows] + (->> rows + parse-rows + (filter assignment-overlaps) + count)) + +(with-open [rdr (io/reader "resources/day04_actual.txt")] + (let [rows (line-seq rdr)] + (do + (println "task1:" (task1 rows)) + (println "task2:" (task2 rows))))) |
