highlife/default
HighLife
- kind
- cellular automaton, 2D
- neighborhood
- Moore, 8 cells
- rule
- B36/S23
- states
- 2
pick a language to open the code
BORN = (3, 6) SURVIVE = (2, 3) 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 high_life_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]; const SURVIVE = [2, 3]; 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 highLifeStep(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)) #define SURVIVE ((1 << 2) | (1 << 3)) 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 high_life_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); constexpr int SURVIVE = (1 << 2) | (1 << 3); 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> high_life_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); const int Survive = (1 << 2) | (1 << 3); 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[] HighLifeStep(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); static final int SURVIVE = (1 << 2) | (1 << 3); 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[] highLifeStep(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; }