algoalgo-world
algoalgo-world/day-and-night
day-and-night/default

낮과 밤

종류
2차원 셀룰러 오토마타
보는 이웃
무어, 여덟 칸
규칙
B3678/S34678
상태
2

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

BORN = (3, 6, 7, 8)
SURVIVE = (3, 4, 6, 7, 8)


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


def day_and_night_step(cells, w, h):
    out = [0] * (w * h)
    for y in range(h):
        for x in range(w):
            live = live_neighbors(cells, w, h, x, y)
            rule = SURVIVE if cells[y * w + x] == 1 else BORN
            out[y * w + x] = 1 if live in rule else 0
    return out
const BORN = [3, 6, 7, 8];
const SURVIVE = [3, 4, 6, 7, 8];

function liveNeighbors(cells, w, h, x, y) {
  let total = 0;
  for (let dy = -1; dy <= 1; dy++) {
    for (let dx = -1; dx <= 1; dx++) {
      if (dx === 0 && dy === 0) continue;
      total += cells[((y + dy + h) % h) * w + ((x + dx + w) % w)];
    }
  }
  return total;
}

function dayAndNightStep(cells, w, h) {
  const out = new Uint8Array(w * h);
  for (let y = 0; y < h; y++) {
    for (let x = 0; x < w; x++) {
      const live = liveNeighbors(cells, w, h, x, y);
      const rule = cells[y * w + x] === 1 ? SURVIVE : BORN;
      out[y * w + x] = rule.includes(live) ? 1 : 0;
    }
  }
  return out;
}
#define BORN ((1 << 3) | (1 << 6) | (1 << 7) | (1 << 8))
#define SURVIVE ((1 << 3) | (1 << 4) | (1 << 6) | (1 << 7) | (1 << 8))

static int live_neighbors(const char cells[], int w, int h, int x, int y) {
    int total = 0;
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx == 0 && dy == 0) continue;
            total += cells[((y + dy + h) % h) * w + (x + dx + w) % w];
        }
    }
    return total;
}

void day_and_night_step(const char cells[], char out[], int w, int h) {
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int live = live_neighbors(cells, w, h, x, y);
            int rule = cells[y * w + x] == 1 ? SURVIVE : BORN;
            out[y * w + x] = (rule >> live) & 1;
        }
    }
}
constexpr int BORN = (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8);
constexpr int SURVIVE = (1 << 3) | (1 << 4) | (1 << 6) | (1 << 7) | (1 << 8);

static int live_neighbors(const std::vector<char>& cells, int w, int h,
                          int x, int y) {
    int total = 0;
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx == 0 && dy == 0) continue;
            total += cells[((y + dy + h) % h) * w + (x + dx + w) % w];
        }
    }
    return total;
}

std::vector<char> day_and_night_step(const std::vector<char>& cells, int w,
                                     int h) {
    std::vector<char> out(cells.size(), 0);
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int live = live_neighbors(cells, w, h, x, y);
            int rule = cells[y * w + x] == 1 ? SURVIVE : BORN;
            out[y * w + x] = (rule >> live) & 1;
        }
    }
    return out;
}
const int Born = (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8);
const int Survive = (1 << 3) | (1 << 4) | (1 << 6) | (1 << 7) | (1 << 8);

static int LiveNeighbors(byte[] cells, int w, int h, int x, int y) {
    int total = 0;
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx == 0 && dy == 0) continue;
            total += cells[((y + dy + h) % h) * w + (x + dx + w) % w];
        }
    }
    return total;
}

static byte[] DayAndNightStep(byte[] cells, int w, int h) {
    byte[] next = new byte[w * h];
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int live = LiveNeighbors(cells, w, h, x, y);
            int rule = cells[y * w + x] == 1 ? Survive : Born;
            next[y * w + x] = (byte)((rule >> live) & 1);
        }
    }
    return next;
}
static final int BORN = (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8);
static final int SURVIVE =
    (1 << 3) | (1 << 4) | (1 << 6) | (1 << 7) | (1 << 8);

static int liveNeighbors(byte[] cells, int w, int h, int x, int y) {
    int total = 0;
    for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
            if (dx == 0 && dy == 0) continue;
            total += cells[((y + dy + h) % h) * w + (x + dx + w) % w];
        }
    }
    return total;
}

static byte[] dayAndNightStep(byte[] cells, int w, int h) {
    byte[] out = new byte[w * h];
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int live = liveNeighbors(cells, w, h, x, y);
            int rule = cells[y * w + x] == 1 ? SURVIVE : BORN;
            out[y * w + x] = (byte) ((rule >> live) & 1);
        }
    }
    return out;
}
돌려 보고 코드도 본다