diff options
author | Leah Neukirchen <leah@vuxu.org> | 2020-12-25 14:35:55 +0100 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2020-12-25 14:35:55 +0100 |
commit | 1047a8d0c88fc686fb3aba8495167b97a80bab8d (patch) | |
tree | 084284cb9a52e37856b91014c2474f885c19df69 /day24.clj | |
parent | 69a013313ee0399e797363bbe76d9b32747c723f (diff) | |
download | adventofcode2020-1047a8d0c88fc686fb3aba8495167b97a80bab8d.tar.gz adventofcode2020-1047a8d0c88fc686fb3aba8495167b97a80bab8d.tar.xz adventofcode2020-1047a8d0c88fc686fb3aba8495167b97a80bab8d.zip |
day24
Diffstat (limited to 'day24.clj')
-rw-r--r-- | day24.clj | 57 |
1 files changed, 57 insertions, 0 deletions
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 |