Functions
WebDev/*nix assorted functions & hacks
Search
find all
Array
-
Bash
-
Classes
-
Config
-
DateTime
-
Debug
-
dotjs
-
File
-
foooo
-
Git
-
HTML
-
Images
-
Javascript
-
Linux
-
Math
-
Misc
-
MySQL
-
OS X
-
Reading
-
Redis
-
Shell scripts
-
snippets
-
SQL
-
Strings
-
TextMate
-
Web
-
\(^o^)/ Fun!
-
_Misc hacks
-
_Pages
\(^o^)/ Fun!
* Readme
Alphanumeric images to ascii
Conway's Game of Life as TextMate command
Langton's Ant
Quine
Sierpiński Carpet
Sierpiński Triangle
Towers of Hanoi
8
found
auth
Name:
Category: \(^o^)/ Fun!
Body:
#!/usr/bin/php <?php /* by default empty cells are a space, live/full cells are 'x' */ $gridStr = ''; while ($fetch = fread(STDIN, 1000)) $gridStr .= $fetch; $c = 'x'; $s = ' '; $settings = false; $firstLine = substr($gridStr, 0, strpos($gridStr, "\n")); if (count(count_chars($firstLine, 1))>2) { $settings = true; $gridStr = substr($gridStr, strpos($gridStr, "\n")+1); foreach (preg_split('/[,;]/', trim($firstLine)) as $pair) { if (preg_match('/[a-z]:[^:]/', $pair)) { ${$pair[0]} = $pair[2]; } } } foreach (explode("\n", trim($gridStr, "\n")) as $line) $grid[] = str_split($line); $lr = count($grid)-1; // index of last row // $lr = max(array_map(function($a){return strlen($a);}, $grid)); // tried to set to longest line, doesn't /quite/ work.. $lc = count($grid[0])-1; // index of last col foreach ($grid as $i => $row) { foreach ($row as $j => $cell) { $prev_i = $i ? $i-1 : $lr; // wrap around coords $prev_j = $j ? $j-1 : $lc; $next_i = ($i < $lr) ? $i+1 : 0; $next_j = ($j < $lc) ? $j+1 : 0; $nbs = // sum neighbours ($grid[$prev_i][$prev_j]==$c) + ($grid[$prev_i][$j]==$c) + ($grid[$prev_i][$next_j]==$c) + ($grid[$i][$prev_j]==$c) + ($grid[$i][$next_j]==$c) + ($grid[$next_i][$prev_j]==$c) + ($grid[$next_i][$j]==$c) + ($grid[$next_i][$next_j]==$c); if ($grid[$i][$j]==$c) { // apply rules if ($nbs < 2 or $nbs > 3) $newGrid[$i][$j] = $s; }else{ if ($nbs == 3) $newGrid[$i][$j] = $c; } } } foreach ($newGrid as $i => $row) { foreach ($row as $j => $cell) { $grid[$i][$j] = $cell; } } if ($settings) echo $firstLine."\n"; foreach ($grid as $row) echo join('', $row)."\n";
Footer