Question 4: A Perl solution
#!/usr/bin/perl
$posx = 0;
$posy = 511;
$minx = 0;
$maxx = 0;
$miny = 0;
$maxy = 0;
$next = 0;
while (<>) {
@chars = split //;
foreach (@chars) {
if ($_ eq 'N') {$posy += 1};
if ($_ eq 'S') {$posy -= 1};
if ($_ eq 'W') {$posx -= 1};
if ($_ eq 'E') {$posx += 1};
if ($_ eq '*') {
$image[$posx][$posy] = 1;
}
else {
$image[$posx][$posy] = 0;
}
$posx > $maxx and $maxx = $posx;
$posx < $minx and $minx = $posx;
$posy > $maxy and $maxy = $posy;
$posy < $miny and $miny = $posy;
}
}
$maxx++;
$maxy++;
print "P1 $maxx $maxy\n";
for ($i=0; $i<$maxx; $i++) {
for ($j=0; $j<$maxy; $j++) {
print $image[$i][$j];
}
print "\n";
}