Skip to content

Instantly share code, notes, and snippets.

@xem
Forked from 140bytes/LICENSE.txt
Last active August 29, 2015 14:02
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 xem/e3c145ab5eb69b3a72db to your computer and use it in GitHub Desktop.
Save xem/e3c145ab5eb69b3a72db to your computer and use it in GitHub Desktop.

Mini Mini Game of life

A game of line engine where everything that can be passed as parameter, is.

Score: 81b.

// Choose an integer d.
// Prepare a square grid of size d * d. And a JS array representing its state with booleans.
// Prepare another JS array that will be edited by this function
// @param s: current state of the grid (array)
// @param n: next state of the grid (another array)
// @param i: number of cells in the grid (i = d * d)
// @param m: neighbours offsets (array filled with: [d + 1, d, d - 1, 1, -1, - d - 1, - d, - d + 1])
// @param j: placeholder
// @param k: placeholder
function(s, n, i, m, j, k){
for(
;
i--; // For each cell
n[i] = k == 3 || s[i] && k == 2, // Set next state (live if it has 3 neighbours or if it lives and has 2 neighbours)
k = 0 // Reset the count of living neighbours
){
for(j in m){ // loop on neighbours offsets
k += s[i + m[j]] // count the living neighbours
}
}
}
function(d,g,c,e,f,b){for(;c--;g[c]=3==b||d[c]&&2==b,b=0)for(f in e)b+=d[c+e[f]]}
@atk
Copy link

atk commented Jun 11, 2014

Could 3==b||d[c]&&2==b be replaced with b==3-d[c]?

Update: no, because if it lives, 3 neighbours will also suffice, but we can do (b|d[c])==3 instead, saving 3 chars to your current solution.

@xem
Copy link
Author

xem commented Jun 12, 2014

Thanks atk! I'll try to use your trick in the whole new version of http://xem.github.io/miniGameOfLife

BTW, as our friends @aemkei and @subzey pointed out, it's nonsense to put as many things as possible in the parameters:

https://twitter.com/MaximeEuziere/status/474633537262333952

:)

@atk
Copy link

atk commented Jun 13, 2014

That'll only work with true/false, not with 0/1, so it might not work with your current approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment