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