Last active
January 5, 2017 15:20
-
-
Save a3f/e3ccfa54046a46b94a98f4781d0c9c3b to your computer and use it in GitHub Desktop.
Transliteration of life←{ ↑1 ⍵∨.∧3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵ } from APL to Perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
my $HZ = 100; | |
################################## | |
use Time::HiRes qw(sleep); | |
use PDL; | |
use PDL::Matrix; | |
use strict; | |
use warnings; | |
my $period = 1/$HZ; | |
sub rotations { ($_->rotate(-1), $_, $_->rotate(1)) } | |
my @data = map { tr/ o\n/01/; [split //] } <DATA>; | |
my $gen = mpdl \@data; | |
while (1) { | |
# prepare for printing | |
my $canvas = $gen =~ s/ //gr =~ tr/01/ o/r; | |
# cursor to top left corner and print every $period | |
print "\033[2J\033[H$canvas"; | |
sleep $period; | |
# replace every cell with a count of neighbours | |
my $neighbourhood = zeroes $gen->dims; | |
$neighbourhood += $_ for map { rotations } map {$_->transpose} | |
map { rotations } $gen->transpose; | |
# next gen are live cells with four neighbours or any with three | |
my $next = $gen & (4 == $neighbourhood) | (3 == $neighbourhood); | |
# procreate | |
$gen = $next; | |
} | |
__DATA__ | |
oo o o | |
oo o o o | |
oo o o ooo | |
oo o ooo | |
o ooo o | |
o o o | |
ooo o o | |
o | |
ooo o | |
o o | |
o o | |
o o | |
ooo o | |
ooo | |
o | |
o o o | |
o o | |
oo | |
ooo | |
ooo | |
o o | |
ooo | |
ooo | |
ooo | |
ooo | |
o o | |
ooo | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment