Skip to content

Instantly share code, notes, and snippets.

@paranoiq
Created November 29, 2022 12:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paranoiq/eb69f5a1b21427ad4ec5b79e93b719f4 to your computer and use it in GitHub Desktop.
Save paranoiq/eb69f5a1b21427ad4ec5b79e93b719f4 to your computer and use it in GitHub Desktop.
Generator of the Supersymmetric Table of Cellular Automata
<?php
error_reporting(E_ALL);
function bin(int $n): string
{
$bin = str_pad(decbin($n), 8, '0', STR_PAD_LEFT);
return substr($bin, 0, 4) . ' ' . substr($bin, 4, 4);
}
$rules = range(0, 255);
$states = [' ', ' #', ' # ', ' ##', '# ', '# #', '## ', '###'];
$borders = 12 + 12 + 8 + 32; // 64
$bodyWidth = 1920 - 64; // 1856
$oneWidth = $bodyWidth / 16; // 116
$bodyHeight = 1080 - 64; // 1016
$oneHeight = $bodyHeight / 16; // 63,5
$width = 115; // 97, 153
$height = 61;
$start = '';
for ($w = 0; $w < $width; $w++) {
$start[$w] = random_int(0, 1) !== 0 ? '#' : ' ';
}
foreach ($rules as $rule) {
$actions = [];
for ($i = 0; $i < 8; $i++) {
$actions[$states[$i]] = ($rule & (2 ** $i)) ? '#' : ' ';
}
$image = imagecreate($width, $height);
$dead = imagecolorallocate($image, 255, 255, 255);
$live = imagecolorallocate($image, 0, 0, 0);
$current = $start;
for ($x = 1; $x <= $width; $x++) {
imagesetpixel($image, $x - 1, 0, $start[$x] === ' ' ? $dead : $live);
}
$ca = [$current];
for ($y = 1; $y < $height; $y++) {
$next = ' ';
for ($x = 1; $x <= $width; $x++) {
$cell = $actions[substr($current, $x - 1, 3)];
$next .= $cell;
imagesetpixel($image, $x - 1, $y, $cell === ' ' ? $dead : $live);
}
$next .= ' ';
$current = $next;
}
imagepng($image, __DIR__ . "/img/{$rule}.png");
}
$sortResults = true;
echo "<html><body>
<style>
img { image-rendering: pixelated; margin: 0px; }
table { border-collapse: collapse }
td { position: relative; font-size: 6pt; font-family: 'pixelFJ8pt1'; }
td .r { position: absolute; bottom: 3px; left: 3px; padding: 0px 2px 0px 2px; background: white; }
td .b { position: absolute; bottom: 3px; right: 3px; padding: 0px 2px 0px 2px; background: white; }
.title { font-size: 12pt; font-family: 'pixelFJ8pt1'; font-smooth: never; }
td { border: 1px black solid; }
td:nth-child(1) { border-left-width: 4px; }
td:nth-child(2n) { border-right-width: 2px; }
td:nth-child(4n) { border-right-width: 3px; }
td:nth-child(8n) { border-right-width: 4px; }
tr:nth-child(1) td { border-top-width: 4px; }
tr:nth-child(2n) td { border-bottom-width: 2px; }
tr:nth-child(4n) td { border-bottom-width: 3px; }
tr:nth-child(8n) td { border-bottom-width: 4px; }
</style>
<pre><code>";
echo "<div class='title'>Supersymmetric Table of Elementary Cellular Automata (@paranoiq 2022)</div>";
echo "<table>";
$rows = [0, 16, 64, 80, 32, 48, 96, 112, 128, 144, 192, 208, 160, 176, 224, 240];
$cols = [0, 2, 8, 10, 4, 6, 12, 14, 1, 3, 9, 11, 5, 7, 13, 15];
foreach ($rows as $row) {
echo "<tr>";
foreach ($cols as $col) {
$rule = $row + $col;
$bin = bin($rule);
echo "<td><div class='r'>{$rule}</div><div class='b'>{$bin}</div><img src='img/{$rule}.png'>";
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment