Tensara x Pace Capital Logo

Cellular Automata

Implement an elementary cellular automata step function for a 2D grid of size n×mn \times m using a von Neumann neighborhood.

The von Neumann neighborhood of a cell (i,j)(i,j) consists of the cell itself and its 4 orthogonal neighbors: (i1,j)(i-1,j) (above), (i+1,j)(i+1,j) (below), (i,j1)(i,j-1) (left), and (i,j+1)(i,j+1) (right).

Given input grid A, compute output grid B by applying your own custom function ff to each cell's von Neumann neighborhood:

Bi,j=f(Ai,j,Ai1,j,Ai+1,j,Ai,j1,Ai,j+1)B_{i,j} = f(A_{i,j}, A_{i-1,j}, A_{i+1,j}, A_{i,j-1}, A_{i,j+1})

Design ff to create the most visually interesting simulation possible!

Input

  • A: Input grid of integers of size n×mn \times m
  • n: Number of rows
  • m: Number of columns

Output

  • B: Output grid of integers of size n×mn \times m

Notes

Use zero-flux (Neumann) boundary conditions at all edges:

  • At i=0i=0 (first row): treat A1,j=A0,jA_{-1,j} = A_{0,j}
  • At i=n1i=n-1 (last row): treat An,j=An1,jA_{n,j} = A_{n-1,j}
  • At j=0j=0 (first column): treat Ai,1=Ai,0A_{i,-1} = A_{i,0}
  • At j=m1j=m-1 (last column): treat Ai,m=Ai,m1A_{i,m} = A_{i,m-1}
Loading...

Loading editor...

CUDA C++ environment

Desktop Required for Code Submission

For the best coding experience, please switch to a desktop device to write and submit your solution.