(ns edition2022.day4
  (:require [clojure.string :as str]))

(defn parse-range [p]
  (map #(Integer/parseInt %) (str/split p #"-")))

(defn parse-ranges [[p1 p2]]
  [(parse-range p1) (parse-range p2)])

(defn read-ranges []
  (let [in-str (slurp "resources/day4.in")
        lines (str/split-lines in-str)
        pairs (map #(str/split % #",") lines)
        ranges (map parse-ranges pairs)]
    ranges))

(defn fully-contains? [[section1 section2]]
  (and
    (<= (first section1) (first section2))
    (>= (second section1) (second section2))))

(defn one-or-other-fully-contains? [sections]
  (or
    (fully-contains? sections)
    (fully-contains? (reverse sections))))

(defn count-fully-contained [ranges]
  (->> ranges
       (filter one-or-other-fully-contains?)
       (count)))

(defn overlaps? [[section1 section2]]
  (and
    (<= (first section1) (first section2))
    (>= (second section1) (first section2))))


(defn one-or-other-overlaps? [sections]
  (or
    (overlaps? sections)
    (overlaps? (reverse sections))))

(defn count-overlapping [ranges]
  (->> ranges
       (filter one-or-other-overlaps?)
       (count)))

(comment
  (fully-contains? [[3 6] [3 4]])
  (fully-contains? [[3 6] [2 4]])
  (fully-contains? [[3 6] [3 7]])
  (fully-contains? [[3 6] [2 7]])
  (one-or-other-fully-contains? [[3 6] [2 7]])
  (read-ranges)
  (count-fully-contained (read-ranges))
  (count-overlapping (read-ranges))
  )