From 1047a8d0c88fc686fb3aba8495167b97a80bab8d Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Fri, 25 Dec 2020 14:35:55 +0100 Subject: day24 --- day24.clj | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 day24.clj (limited to 'day24.clj') diff --git a/day24.clj b/day24.clj new file mode 100644 index 0000000..0e57d2a --- /dev/null +++ b/day24.clj @@ -0,0 +1,57 @@ +(def data + (->> (slurp "day24") + (clojure.string/split-lines))) + +; nw ne 1,-1 1,0 +; w . e 0,-1 0,0 0,1 +; sw se -1,0 -1,1 + +(def dirs + {"e" [0 1] + "se" [-1 1] + "sw" [-1 0] + "w" [0 -1] + "nw" [1 -1] + "ne" [1 0]}) + +(defn pos [l] + (->> l + (re-seq #"e|se|sw|w|nw|ne") + (map dirs) + (apply (partial mapv +)))) + +(def black + (set + (map first (filter (fn [[k v]] (odd? v)) (frequencies (map pos data)))))) + +(count black) +;; => 386 + + +(defn all-neighbours [cell] + (for [offset (vals dirs)] + (map + offset cell))) + +(defn alive-neighbours [cells cell] + (filter cells (all-neighbours cell))) + +(defn dead-neighbours [cells cell] + (filter (complement cells) (all-neighbours cell))) + +(defn regulate [cells] + (filter #(#{1 2} (count (alive-neighbours cells %))) cells)) + +(defn dead-neighbour-cells [cells] + (reduce clojure.set/union (map (partial dead-neighbours cells) cells))) + +(defn reproduce [cells] + (filter #(= 2 (count (alive-neighbours cells %))) (dead-neighbour-cells cells))) + +(defn tick [cells] + (clojure.set/union (set (reproduce cells)) (set (regulate cells)))) + +(->> black + (iterate tick) + (#(nth % 100)) + count) +;; => 4214 -- cgit 1.4.1