aboutsummaryrefslogtreecommitdiffstats
path: root/src/aoc_2022/day04.clj
blob: bd4d562c7275d96a1787e3e3402682f569a5fc05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(ns aoc-2022.day04)
(require '[aoc-2022.utils :refer :all])
(require '[clojure.java.io :as io])
(require '[clojure.string :as str])

(defn parse-rows [rows]
  (->> rows
       (map #(str/split % #","))
       (map (fn [[a, b]]
              [(map to-long (str/split a #"-")),
               (map to-long (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))))

(def task2
  (chain
   parse-rows
   (partial 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))
      :done)))