Skip to content

Instantly share code, notes, and snippets.

@moritz
Created March 22, 2011 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moritz/882070 to your computer and use it in GitHub Desktop.
Save moritz/882070 to your computer and use it in GitHub Desktop.
use v6;
sub transformed_field(@field) {
my %transform = ('*' => '*', '.' => 0);
my @t = @field.map: { [ .map: { %transform{$_}} ] };
for @t.keys X @t[0].keys -> $x, $y {
next unless @t[$x][$y] eq '*';
for ($x - 1 max 0) .. ($x + 1 min @t.end)
X ($y - 1 max 0) .. ($y + 1 min @t[0].end) -> $xa, $ya {
@t[$xa][$ya]++ if @t[$xa][$ya] ne '*';
}
}
@t>>.join.join("\n");
}
sub MAIN {
my $count = 1;
loop {
my ($rows, $cols) = get.words;
last if $rows == 0 && $cols == 0;
my @field = ([get.comb] for ^$rows);
say '' if $count != 1;
say "Field #$count:";
$count++;
say transformed_field @field;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment