algoalgo-world
algoalgo-world/cyclic
cyclic/default

순환 오토마타

종류
2차원 셀룰러 오토마타
보는 이웃
무어, 여덟 칸
규칙
다음 상태를 가진 이웃이 하나라도 있으면 그 상태가 된다
상태
8

언어를 고르면 코드가 열린다

def any_neighbor(cells, w, h, x, y, want):
    for dy in (-1, 0, 1):
        for dx in (-1, 0, 1):
            if dx == 0 and dy == 0:
                continue
            if cells[(y + dy) % h * w + (x + dx) % w] == want:
                return True
    return False


def cyclic_step(cells, w, h, states):
    out = [0] * (w * h)
    for y in range(h):
        for x in range(w):
            here = cells[y * w + x]
            want = (here + 1) % states
            if any_neighbor(cells, w, h, x, y, want):
                out[y * w + x] = want
            else:
                out[y * w + x] = here
    return out
function anyNeighbor(cells, w, h, x, y, want) {
  for (let dy = -1; dy <= 1; dy++) {
    for (let dx = -1; dx <= 1; dx++) {
      if (dx === 0 && dy === 0) continue;
      const at = ((y + dy + h) % h) * w + ((x + dx + w) % w);
      if (cells[at] === want) return true;
    }
  }
  return false;
}

function cyclicStep(cells, w, h, states) {
  const out = new Uint8Array(w * h);
  for (let y = 0; y < h; y++) {
    for (let x = 0; x < w; x++) {
      const here = cells[y * w + x];
      const want = (here + 1) % states;
      out[y * w + x] = anyNeighbor(cells, w, h, x, y, want) ? want : here;
    }
  }
  return out;
}
static int any_neighbor(const char cells[], int w, int h, int x, int y,
                        int want) {
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx == 0 && dy == 0) continue;
            int at = ((y + dy + h) % h) * w + (x + dx + w) % w;
            if (cells[at] == want) return 1;
        }
    }
    return 0;
}

void cyclic_step(const char cells[], char out[], int w, int h, int states) {
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int here = cells[y * w + x];
            int want = (here + 1) % states;
            int found = any_neighbor(cells, w, h, x, y, want);
            out[y * w + x] = found ? want : here;
        }
    }
}
static bool any_neighbor(const std::vector<char>& cells, int w, int h,
                         int x, int y, int want) {
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx == 0 && dy == 0) continue;
            int at = ((y + dy + h) % h) * w + (x + dx + w) % w;
            if (cells[at] == want) return true;
        }
    }
    return false;
}

std::vector<char> cyclic_step(const std::vector<char>& cells, int w, int h,
                              int states) {
    std::vector<char> out(cells.size(), 0);
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int here = cells[y * w + x];
            int want = (here + 1) % states;
            bool found = any_neighbor(cells, w, h, x, y, want);
            out[y * w + x] = found ? want : here;
        }
    }
    return out;
}
static bool AnyNeighbor(byte[] cells, int w, int h, int x, int y, int want) {
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx == 0 && dy == 0) continue;
            int at = ((y + dy + h) % h) * w + (x + dx + w) % w;
            if (cells[at] == want) return true;
        }
    }
    return false;
}

static byte[] CyclicStep(byte[] cells, int w, int h, int states) {
    byte[] next = new byte[w * h];
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int here = cells[y * w + x];
            int want = (here + 1) % states;
            bool found = AnyNeighbor(cells, w, h, x, y, want);
            next[y * w + x] = (byte)(found ? want : here);
        }
    }
    return next;
}
static boolean anyNeighbor(byte[] cells, int w, int h, int x, int y,
                           int want) {
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx == 0 && dy == 0) continue;
            int at = ((y + dy + h) % h) * w + (x + dx + w) % w;
            if (cells[at] == want) return true;
        }
    }
    return false;
}

static byte[] cyclicStep(byte[] cells, int w, int h, int states) {
    byte[] out = new byte[w * h];
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int here = cells[y * w + x];
            int want = (here + 1) % states;
            boolean found = anyNeighbor(cells, w, h, x, y, want);
            out[y * w + x] = (byte) (found ? want : here);
        }
    }
    return out;
}
돌려 보고 코드도 본다