<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#! /usr/bin/perl
# fb.pl
# Solve fencebuilder problem.
# Start: Sat Sep  6 14:40:33 EDT 1997

$state=0 ; # 0=new prog;

%struts = ();
# list of coords of all lower-left hand square metres that are fenced in.



sub min { # 2 arg min
	local($a,$b)=@_;
	$a &lt; $b ? $a : $b;
}

sub init {
	$x=0;
	$y=0;
	%struts=();
}

sub mark {
	local($a,$b,$c,$d)=@_;
	local($mx)=$a; # == $c;
	local($my)=&amp;min($b,$d);
	if ( length($struts{$my}) &lt;= 0 ) { $struts{$my} = $mx; }
	else { $struts{$my} .= ";$mx"; }
}

sub bynumber { $a &lt;=&gt; $b; }
sub answer {
	local($y,@xl,$total,$p, @xln);
	$total = 0;
	foreach $i ( sort keys(%struts) ) {
		$p = $struts{$i};
# print "$i: $p\n";
	
		@xl=sort bynumber split(/;/,$p);
		while ( $#xl &gt; 0 ) {
			local($l,$r);
			$l = shift(@xl);
			$r = shift(@xl);
			$total += $r - $l;
		}
	}
	print "$total\n";
}

sub n { &amp;mark($x,$y,$x,$y+1); $y++; }
sub s { &amp;mark($x,$y,$x,$y-1); $y--; }
sub e {                       $x++; }
sub w {                       $x--; }

&amp;init;
while($line = &lt;&gt;) {
	if ( $line=~ m/^\./ ) {
		&amp;answer;
		&amp;init;
	} elsif ( $line=~ m/^n/ ) { &amp;n; }
	  elsif ( $line=~ m/^e/ ) { &amp;e; }
	  elsif ( $line=~ m/^w/ ) { &amp;w; }
	  elsif ( $line=~ m/^s/ ) { &amp;s; }
}
</pre></body></html>