[kw-pm] Mystery code!

Eric - fishbot eric at uc.org
Fri Sep 17 10:55:28 CDT 2004


Lloyd,

Enjoyed your presentation last night... I am looking forward to
you posting your mystery code.  Please let us know when it is up.

Without spoiling the mystery for those who failed to attend last
night, I had some thoughts about determining non-adjacency.  (I
had insomnia last night, and got out some paper.)

It seems that using a 2D array, it would be fairly easy to
determine adjacency... anything that is +-[1|0,1|0] away.  If you
pushed these adjacencies into an hashofArray, then a simple
nested loop can create the regex of non-adjacencies.

I figure that this is probably fifteen lines of code to generate
the regex which you could spell out manually in one or two...
which solution would be more "graceful" is up for discussion.

# given %adjacent{$i} is array of numbers adjacent to $i:

my @illegal_pairs;

for my $i ( 2..9 ) {
    INNER: for my $j ( 2..9 ) {
        next INNER if (( $i == $j ) ||
                       ( grep { $_ == $j } @{$adjacent{$i}} ));
        push @illegal_pairs, "$i$j";
    }
}

my $regex = join "|", @illegal_pairs;


However, to generate the list of adjacencies, I think that you
would pretty much need a triple-nested-loop, which is hard to say
is graceful.

Perhaps someone can come up with a sexy map{} for the problem?

Eric


More information about the kw-pm mailing list