From fe08bce34f4c4839d56cbb14c4132df68d92b733 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Sun, 13 Dec 2020 22:24:55 +0100 Subject: day11 part 2 --- day11.clj | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 day11.clj (limited to 'day11.clj') diff --git a/day11.clj b/day11.clj new file mode 100644 index 0000000..73191ad --- /dev/null +++ b/day11.clj @@ -0,0 +1,54 @@ +(def data + (->> (slurp "day11") + (clojure.string/split-lines) + (map vec) + vec)) + +(defn in-bound [[x y]] + (and (< -1 x (count data)) + (< -1 y (count (first data))))) + +(defn fix [l] + (->> l + (partition 2 1) + (drop-while (fn [[x y]] (not= x y))) + ffirst)) + +(defn gneighbors [ch? [x y]] + (->> (for [dir (for [dx [-1 0 1] + dy [-1 0 1] + :when (not= 0 dx dy)] + [dx dy])] + (->> [x y] + (iterate (partial mapv + dir)) + (drop 1) + (take-while in-bound) + (map (fn [p] [p (get-in data p)])) + (some (fn [[p c]] (if (ch? c) p))))) + (remove nil?))) + +(defn gstep [neighbors ncnt data] + (vec (for [x (range (count data))] + (vec (for [y (range (count (first data)))] + (let [occ (->> (neighbors [x y]) + (map (partial get-in data)) + (filter #{\#}) + count)] + (case (get-in data [x y]) + \. \. + \L (if (zero? occ) \# \L) + \# (if (>= occ ncnt) \L \#)))))))) + +(->> data + (iterate (partial gstep (partial gneighbors #{\L \# \.}) 4)) + fix + flatten + (filter #{\#}) + count) ; => 2324 + +(->> data + (iterate (partial gstep (partial gneighbors #{\L \#}) 5)) + fix + flatten + (filter #{\#}) + count) ; => 2068 -- cgit 1.4.1