about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2020-12-18 12:14:35 +0100
committerLeah Neukirchen <leah@vuxu.org>2020-12-18 12:14:35 +0100
commite40c307a686af0476d2dd8ceca29117bf5181b5c (patch)
tree40b70cb8f13c5d99357bd978e7ae83584072eea8
parent8afe06a504283d2408f1fe5cdd684e56d0fa945f (diff)
downloadadventofcode2020-e40c307a686af0476d2dd8ceca29117bf5181b5c.tar.gz
adventofcode2020-e40c307a686af0476d2dd8ceca29117bf5181b5c.tar.xz
adventofcode2020-e40c307a686af0476d2dd8ceca29117bf5181b5c.zip
day17
-rw-r--r--day178
-rw-r--r--day17.clj69
2 files changed, 77 insertions, 0 deletions
diff --git a/day17 b/day17
new file mode 100644
index 0000000..04e8187
--- /dev/null
+++ b/day17
@@ -0,0 +1,8 @@
+..#..##.
+#.....##
+##.#.#.#
+..#...#.
+.###....
+######..
+.###..#.
+..#..##.
diff --git a/day17.clj b/day17.clj
new file mode 100644
index 0000000..b2f66fb
--- /dev/null
+++ b/day17.clj
@@ -0,0 +1,69 @@
+(def data
+  (->> (slurp "day17")
+       clojure.string/split-lines))
+
+(def cells
+  (set
+   (remove nil?
+           (for [y (range (count data))
+                 x (range (count (first data)))]
+             (if (= \# (get-in data [y x]))
+               (list 0 y x))))))
+
+(def neighbour-offsets
+  (let [digits [-1 0 1]]
+    (for [x digits
+          y digits
+          z digits
+          :when (not= 0 x y z)]
+      [z y x])))
+
+(defn all-neighbours [cell]
+  (for [offset neighbour-offsets]
+    (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 #(#{2 3} (count (alive-neighbours cells %))) cells))
+
+(defn dead-neighbour-cells [cells]
+  (reduce clojure.set/union (map (partial dead-neighbours cells) cells)))
+
+(defn reproduce [cells]
+  (filter #(= 3 (count (alive-neighbours cells %))) (dead-neighbour-cells cells)))
+
+(defn tick [cells] 
+  (clojure.set/union (set (reproduce cells)) (set (regulate cells))))
+
+
+(->> cells
+     (iterate tick)
+     (drop 6)
+     first
+     count)
+;; => 298
+
+
+(def cells
+  (set (map (partial cons 0) cells)))
+
+(def neighbour-offsets
+  (let [digits [-1 0 1]]
+    (for [x digits
+          y digits
+          z digits
+          w digits
+          :when (not= 0 x y z w)] 
+      [w z y x])))
+
+(->> cells
+     (iterate tick)
+     (drop 6)
+     first
+     count)
+;; => 1792