Skip to content

Instantly share code, notes, and snippets.

@danielcompton danielcompton/j.clj
Last active Aug 29, 2015

Embed
What would you like to do?
(ns gol.core
(:refer-clojure :exclude [* - + == /])
(:require [clojure.core.matrix :refer :all]
[clojure.core.matrix.operators :refer :all]))
(def start [[0 0 0 0 0]
[0 0 0 0 0]
[0 0 1 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
)
(defn live-or-die [neighbor-count]
(case neighbor-count
2 1
3 1
0))
(defn neighbours
[[x y]]
(for [dx [-1 0 1] dy [-1 0 1] :when (not= 0 dx dy)]
[(+ dx x) (+ dy y)]))
(defn harvest
"Takes the count of neighbors at each point, returns a matrix of citizens"
[m]
(emap live-or-die m))
(defn safe-get [m row col]
(try (mget m row col)
(catch Exception e 0)))
(defn count-neighbors
"Takes a matrix of citizens, returns a matrix with the count of neighbors at each point"
[m]
(let [[rows cols] (shape m)
counts (for [row (range rows)
col (range cols)]
(apply + (map (partial apply safe-get start) (neighbours [row col]))))]
(partition (row-count m) counts)))
(defn tick [map])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.