Cellular Automata

Created April 9, 2025 (Today)Updated April 9, 2025 (Today)

Definining Characteristics

  • Cell - the individual unit of the CA
  • State - the value of a cell
  • Neighborhood - which cells interact with a given cell
  • Rules - how the next generation is determined from the current generation

1D Automata

click to restart

  • Neighborhood: 3 cells
  • Cell State: 0 or 1
  • Neighborhood Combindations: 2^3 = 8
  • Possible Rulesets: 2^8 = 256

Example Ruleset:

  • 000 -> 0
  • 001 -> 1
  • 010 -> 0
  • 011 -> 0
  • 100 -> 1
  • 101 -> 1
  • 110 -> 0
  • 111 -> 1

In Code:

/**
 * Calculates the next state of a cell in a 1D cellular automaton based on its neighborhood
 * @param {number} left - The state of the left neighbor (0 or 1)
 * @param {number} state - The current state of the cell (0 or 1)
 * @param {number} right - The state of the right neighbor (0 or 1)
 * @param {number} rule - The rule number to apply (0-255, default 110)
 * @returns {number} The next state of the cell (0 or 1)
 *
 * The rule parameter encodes the output for each possible 3-bit neighborhood:
 * For rule 110:
 * 111 -> 0  (bit 7)
 * 110 -> 1  (bit 6)
 * 101 -> 1  (bit 5)
 * 100 -> 0  (bit 4)
 * 011 -> 1  (bit 3)
 * 010 -> 1  (bit 2)
 * 001 -> 1  (bit 1)
 * 000 -> 0  (bit 0)
 */
function calculateState(left, state, right, rule = 110) {
    const idx = (left << 2) | (state << 1) | right
    return (rule >> idx) & 1
}

Resources