Skip to content

Instantly share code, notes, and snippets.

@minikomi
Created February 14, 2012 09:04
Show Gist options
  • Save minikomi/1824975 to your computer and use it in GitHub Desktop.
Save minikomi/1824975 to your computer and use it in GitHub Desktop.
game of life
(defn gol [ar](->>
(map
(fn [el] ; <= gets all neighbors
(let [x (first el) y (last el)]
(filter
(fn [s]
(and
(not-any? neg? s)
(not-any? #(< (dec (count ar)) %) s)))
(partition 2
(interleave
(map #(+ x %)[-1 0 1 -1 1 -1 0 1])
(map #(+ y %)[-1 -1 -1 0 0 1 1 1]))))))
(partition 2 (flatten (map #(interleave (repeat %)(range (count ar)))(range (count ar))))))
(map
#(map (fn[el]
(let [x (first el) y (last el)]
(get (get ar x) y)))%))
(map #(filter (fn [i] (= \# i))%))
(map count)
(interleave (flatten (map vec ar)))
(partition 2)
(map
(fn [pair]
(let [now (first pair) c (last pair)]
(cond
(and (= \# now) (> 2 c)) \space
(and (= \# now) (or (= 2 c)(= 3 c))) \#
(and (= \# now) (< 3 c)) \space
(and (= \space now) (= 3 c)) \#
:else \space))))
(partition (count ar))
(map #(apply str %))
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment