algoalgo-world
algoalgo-world/rule
rule/default

基本元胞自动机

类型
一维元胞自动机
看的邻居
半径 1,三格
状态
2
规则总数
256

选一种语言就能看到代码

RULE = 30


def next_row(row, w):
    out = [0] * w
    for x in range(w):
        left = row[(x - 1) % w]
        here = row[x]
        right = row[(x + 1) % w]
        pattern = left * 4 + here * 2 + right
        out[x] = RULE >> pattern & 1
    return out
const RULE = 30;

function nextRow(row, w) {
  const out = new Uint8Array(w);
  for (let x = 0; x < w; x++) {
    const left = row[(x - 1 + w) % w];
    const here = row[x];
    const right = row[(x + 1) % w];
    const pattern = left * 4 + here * 2 + right;
    out[x] = (RULE >> pattern) & 1;
  }
  return out;
}
#define RULE 30

void next_row(const char row[], char out[], int w) {
    for (int x = 0; x < w; x++) {
        int left = row[(x - 1 + w) % w];
        int here = row[x];
        int right = row[(x + 1) % w];
        int pattern = left * 4 + here * 2 + right;
        out[x] = (RULE >> pattern) & 1;
    }
}
constexpr int RULE = 30;

std::vector<char> next_row(const std::vector<char>& row) {
    int w = static_cast<int>(row.size());
    std::vector<char> out(w, 0);
    for (int x = 0; x < w; x++) {
        int left = row[(x - 1 + w) % w];
        int here = row[x];
        int right = row[(x + 1) % w];
        int pattern = left * 4 + here * 2 + right;
        out[x] = (RULE >> pattern) & 1;
    }
    return out;
}
const int Rule = 30;

static byte[] NextRow(byte[] row) {
    int w = row.Length;
    byte[] next = new byte[w];
    for (int x = 0; x < w; x++) {
        int left = row[(x - 1 + w) % w];
        int here = row[x];
        int right = row[(x + 1) % w];
        int pattern = left * 4 + here * 2 + right;
        next[x] = (byte)((Rule >> pattern) & 1);
    }
    return next;
}
static final int RULE = 30;

static byte[] nextRow(byte[] row) {
    int w = row.length;
    byte[] out = new byte[w];
    for (int x = 0; x < w; x++) {
        int left = row[(x - 1 + w) % w];
        int here = row[x];
        int right = row[(x + 1) % w];
        int pattern = left * 4 + here * 2 + right;
        out[x] = (byte) ((RULE >> pattern) & 1);
    }
    return out;
}
先跑一遍,再读源码