diff options
author | Leah Neukirchen <leah@vuxu.org> | 2020-12-13 22:24:55 +0100 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2020-12-13 22:24:55 +0100 |
commit | fe08bce34f4c4839d56cbb14c4132df68d92b733 (patch) | |
tree | c1c95abe58ce2460eac8edb93b0e4f0ebdb2c941 /day11.clj | |
parent | 6f632d4daff51d874e049bcdd168366fd1b4c71c (diff) | |
download | adventofcode2020-fe08bce34f4c4839d56cbb14c4132df68d92b733.tar.gz adventofcode2020-fe08bce34f4c4839d56cbb14c4132df68d92b733.tar.xz adventofcode2020-fe08bce34f4c4839d56cbb14c4132df68d92b733.zip |
day11 part 2
Diffstat (limited to 'day11.clj')
-rw-r--r-- | day11.clj | 54 |
1 files changed, 54 insertions, 0 deletions
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 |