Created
January 18, 2021 00:18
-
-
Save ztellman/43b2f00aaebf8c196ff90518ba616d57 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns advent17.core | |
(:require | |
[clojure.java.io :as io] | |
[clojure.string :as str])) | |
(defn neighbors [[x y z w]] | |
(-> | |
(for [x' (range (dec x) (+ 2 x)) | |
y' (range (dec y) (+ 2 y)) | |
z' (range (dec z) (+ 2 z)) | |
w' (range (dec w) (+ 2 w))] | |
[x' y' z' w']) | |
set | |
(disj [x y z w]))) | |
(defn next-state [coords] | |
(let [num-active-neighbors (fn [coord] | |
(->> coord | |
neighbors | |
(filter #(contains? coords %)) | |
count))] | |
(->> coords | |
(mapcat neighbors) | |
set | |
(filter | |
(fn [coord] | |
(let [n (num-active-neighbors coord)] | |
(or | |
(= 3 n) | |
(and | |
(contains? coords coord) | |
(= 2 n)))))) | |
set))) | |
(defn parse [s] | |
(->> s | |
java.io.StringReader. | |
java.io.BufferedReader. | |
line-seq | |
(map-indexed | |
(fn [y l] | |
(->> l | |
str/trim | |
(map-indexed | |
(fn [x c] | |
(when (= c \#) | |
[x y 0 0])))))) | |
(apply concat) | |
(remove nil?))) | |
(def input | |
"##.##### | |
#.##..#. | |
.##...## | |
###.#... | |
.####### | |
##....## | |
###.###. | |
.#.#.#..") | |
(comment | |
".#. | |
..# | |
###" | |
(def coords #{[1 0 0] [2 1 0] [0 2 0] [1 2 0] [2 2 0]})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment