Skip to content

Instantly share code, notes, and snippets.

@danielcompton
Last active August 29, 2015 14:10
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 danielcompton/4e7128111f313df78374 to your computer and use it in GitHub Desktop.
Save danielcompton/4e7128111f313df78374 to your computer and use it in GitHub Desktop.
(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