Langton's ant: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: shrink output by 50%)
(→‎{{header|Perl 6}}: use enums, hyperop, gather/take, given/when)
Line 1,360: Line 1,360:
In this version we use 4-bits-per-char graphics to shrink the output by 50%.
In this version we use 4-bits-per-char graphics to shrink the output by 50%.
<lang perl6>my @dirs = [1,0], [0,-1], [-1,0], [0,1];
<lang perl6>my @dirs = [1,0], [0,-1], [-1,0], [0,1];
constant @char = ' ▘▝▀▖▌▞▛▗▚▐▜▄▙▟█'.comb;
constant @blocky = ' ▘▝▀▖▌▞▛▗▚▐▜▄▙▟█'.comb;
constant $size = 100;
constant $size = 100;
enum Square <White Black>;
my @plane = [0 xx $size] xx $size;
my @plane = [White xx $size] xx $size;
my ($x, $y) = $size/2, $size/2;
my ($x, $y) = $size/2, $size/2;
my $dir = (^4).pick;
my $dir = (^4).pick;
loop (my $move = 0; 0 <= $x < $size and 0 <= $y < $size; $move++) {
loop (my $move = 0; 0 <= $x < $size and 0 <= $y < $size; $move++) {
if @plane[$x][$y] = 1 - @plane[$x][$y] {
given @plane[$x][$y] {
$dir = ($dir + 1) % @dirs;
when White { $dir++; $_ = Black; }
when Black { $dir--; $_ = White; }
} else {
$dir = ($dir - 1) % @dirs;
}
}
$x += @dirs[$dir][0];
($x,$y) »+=« @dirs[$dir %= @dirs];
$y += @dirs[$dir][1];
}
}
print "Out of bounds after $move moves at ($x, $y)\n";
print "Out of bounds after $move moves at ($x, $y)\n";
for 0,2,4 ...^ $size - 2 -> $x {
for 0,2,4 ...^ $size - 2 -> $x {
for 0,2,4 ... $size - 2 -> $y {
say join '', gather for 0,2,4 ... $size - 2 -> $y {
my $sum = @plane[$x][$y]
take @blocky[ 1 * @plane[$x][$y]
+ 2 * @plane[$x][$y+1]
+ 2 * @plane[$x][$y+1]
+ 4 * @plane[$x+1][$y]
+ 4 * @plane[$x+1][$y]
+ 8 * @plane[$x+1][$y+1];
+ 8 * @plane[$x+1][$y+1] ];
print @char[$sum];
}
}
print "\n";
}</lang>
}</lang>
{{out}}
{{out}}