Skip to content

Instantly share code, notes, and snippets.

@ztellman
Created January 18, 2021 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ztellman/43b2f00aaebf8c196ff90518ba616d57 to your computer and use it in GitHub Desktop.
Save ztellman/43b2f00aaebf8c196ff90518ba616d57 to your computer and use it in GitHub Desktop.
(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