@ 2017-07-11T15:09:11Z <?php
/**
* Run it: php r30.php
* Run it with a custom number of rows: php r30.php <number>
* Have fun :)
*/
function createRows($numRows, $rule) {
$getNextRow = function ($currRow) use ($rule) {
$nextRow = [0, 0];
for ($i = 1, $iMax = count($currRow) - 1; $i < $iMax; $i++) {
$nextRow[] = $rule[$currRow[$i - 1] . $currRow[$i] . $currRow[$i + 1]];
}
array_push($nextRow, 0, 0);
return $nextRow;
};
$rows[0] = [0, 0, 1, 0, 0];
for ($i = 1; $i <= $numRows; $i++) {
$rows[$i] = $getNextRow($rows[$i - 1]);
}
return $rows;
}
function displayRows($rows, $isDownwards, $micSleep) {
$rowCount = count($rows);
$getRow = function ($rows, $isDownwards) use ($rowCount) {
if ($isDownwards === true) {
for ($i = 0; $i < $rowCount; $i++) {
yield [$i, $rows[$i]];
}
} else {
for ($i = $rowCount - 1; $i >= 0; $i--) {
yield [$i, $rows[$i]];
}
}
}
foreach ($getRow($rows, $isDownwards) as [$pad, $row]) {
echo str_repeat(' ', $rowCount - $pad);
foreach ($row as $cell) {
echo $cell === 1 ? '█' : ' ';
}
echo PHP_EOL;
usleep($micSleep);
}
}
// https://en.wikipedia.org/wiki/Rule_30
const RULE_30 = [
'111' => 0,
'110' => 0,
'101' => 0,
'100' => 1,
'011' => 1,
'010' => 1,
'001' => 1,
'000' => 0,
];
$numRows = isset($argv[1]) && (int)$argv[1] > 0 && (int)$argv[1] <= 1000 ? (int)$argv[1] : 80;
$rows = createRows($numRows, RULE_30);
while (true) {
displayRows($rows, true, 5000);
displayRows($rows, false, 5000);
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 7.1.7 0.007 0.009 17.04 7.1.6 0.027 0.009 35.14 7.1.5 0.035 0.006 34.41 7.1.4 0.019 0.013 34.45 7.1.3 0.020 0.016 34.31 7.1.2 0.027 0.011 34.58 7.1.1 0.014 0.006 16.48 7.1.0 0.009 0.012 16.43 7.0.20 0.007 0.007 16.67 7.0.19 0.009 0.010 16.42 7.0.18 0.006 0.011 16.02 7.0.17 0.006 0.012 16.09 7.0.16 0.006 0.014 16.04 7.0.15 0.000 0.017 15.99 7.0.14 0.010 0.010 15.91 7.0.13 0.006 0.014 16.39 7.0.12 0.006 0.013 16.53 7.0.11 0.004 0.015 15.89 7.0.10 0.003 0.016 16.08 7.0.9 0.003 0.017 16.01 7.0.8 0.011 0.009 16.09 7.0.7 0.008 0.012 16.15 7.0.6 0.019 0.000 16.06 7.0.5 0.006 0.015 16.41 7.0.4 0.010 0.012 16.20 7.0.3 0.006 0.014 16.37 7.0.2 0.006 0.011 16.20 7.0.1 0.015 0.004 16.22 7.0.0 0.014 0.003 16.33
preferences:dark mode live preview
142.7 ms | 1394 KiB | 7 Q