Forum Discussion
Utilizing Excel's turing capabilities to create Conway's 'Game of Life'
Here are the timings for each solution:
timer used: vba - fullrecalc
100 iterations - average of 5 times
Original - 3.33 seconds
PB - .6189 seconds
MAKEARRAY- .8585 seconds
- on a 36x150 board - 10 iterations - about 8 seconds
Patrick2788 Again a fun and interesting challenge but I went a very different route (I know imagine that). Here is my solution without any LAMBDA inside:
LIFE=LAMBDA(matrix, iterations,
LET(
matrix2, IF(iterations > 1, Life(matrix, iterations - 1), matrix),
m, IFERROR(VSTACK(0, HSTACK(0, matrix2, 0), 0), 0),
neighbors, VSTACK(0, HSTACK(0, m)) + VSTACK(0, m) + VSTACK(0, DROP(m, , 1)) + DROP(m, , 1) +
HSTACK(0, m) + HSTACK(0, DROP(m, 1)) + DROP(m, 1) + DROP(m, 1, 1),
alive_neighbors, neighbors * m,
--DROP(
DROP(((neighbors = 3) + ((alive_neighbors > 1) * (alive_neighbors < 4)) > 0), 1, 1),
-2,
-2
)
)
- Patrick2788Feb 02, 2024Silver Contributor
I like the way you terminate the loop immediately within the first parameter ('matrix2') of LET. My usual go-to is terminating the function with an IF before the function is called in predicable fashion. It's good to see it done smoothly with your formula.
Pad the matrix to make sure each cell has 8 adjacent cells and some clever stacking and addition to find the neighbors. The use of DROP here is a throwback to ctrl+shift+enter arrays and it's elegant here and no logical IF/AND/OR needed! I like the economy of this formula. Thanks for sharing!