day 5
This commit is contained in:
parent
8993238612
commit
b704812923
70
src/day5.clj
Normal file
70
src/day5.clj
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
(ns day5
|
||||||
|
(:require [clojure.edn :as edn]
|
||||||
|
[clojure.string :as string]
|
||||||
|
[helpers :refer [get-input]]))
|
||||||
|
|
||||||
|
(def test-input
|
||||||
|
"3-5
|
||||||
|
10-14
|
||||||
|
16-20
|
||||||
|
12-18
|
||||||
|
|
||||||
|
1
|
||||||
|
5
|
||||||
|
8
|
||||||
|
11
|
||||||
|
17
|
||||||
|
32")
|
||||||
|
|
||||||
|
(defn- parse [input]
|
||||||
|
(let [[rs is] (string/split input #"\n\n")]
|
||||||
|
[(map (fn [r] (mapv edn/read-string (string/split r #"-")))
|
||||||
|
(string/split-lines rs))
|
||||||
|
(map edn/read-string (string/split-lines is))]))
|
||||||
|
|
||||||
|
;;(def input (parse test-input))
|
||||||
|
(def input (parse (get-input 5)))
|
||||||
|
|
||||||
|
(def ranges (first input))
|
||||||
|
(def ingredients (second input))
|
||||||
|
|
||||||
|
(defn- fresh? [i]
|
||||||
|
(->> (filter (comp (partial > i) first) ranges)
|
||||||
|
(filter (comp (partial <= i) second))
|
||||||
|
count
|
||||||
|
pos?))
|
||||||
|
|
||||||
|
(println "Result:"
|
||||||
|
(count (filter fresh? ingredients)))
|
||||||
|
|
||||||
|
(defn- overlaps? [[a1 b1] [a2 b2]]
|
||||||
|
(or (and (>= a2 a1) (<= a2 b1))
|
||||||
|
(and (>= a1 a2) (<= a1 b2))))
|
||||||
|
|
||||||
|
(defn- overlap [rs]
|
||||||
|
[(apply min (map first rs))
|
||||||
|
(apply max (map second rs))])
|
||||||
|
|
||||||
|
(defn- find-overlapping [rs]
|
||||||
|
(map #(filter (partial overlaps? %) rs) rs))
|
||||||
|
|
||||||
|
(defn- process [rs]
|
||||||
|
(set
|
||||||
|
(map overlap (find-overlapping rs))))
|
||||||
|
|
||||||
|
(defn- has-overlapping? [rs]
|
||||||
|
(->>
|
||||||
|
(map count (find-overlapping rs))
|
||||||
|
(some (partial < 1))))
|
||||||
|
|
||||||
|
(defn- count-ingredients [[a b]]
|
||||||
|
(inc (- b a)))
|
||||||
|
|
||||||
|
(println "Result (part 2):"
|
||||||
|
(->>
|
||||||
|
(loop [ranges ranges]
|
||||||
|
(if (has-overlapping? ranges)
|
||||||
|
(recur (process ranges))
|
||||||
|
ranges))
|
||||||
|
(map count-ingredients)
|
||||||
|
(reduce +)))
|
||||||
Loading…
x
Reference in New Issue
Block a user