MGenerative


Fishbot's attempt at a generative solution to the Mystery problem. This code is intentionally undocumented, so as not to spoil mystery. And I am lazy.

Daniel has a non-generative solution: MData. Obviously, in the real world, his solution is the right one. However, my solution can be generalized to determine cell adjacency for arbitrarily large grids.

If I get around to it, my third iteration will factor out the constants and generate the pad. Thus you could just ask for a 10x10 pad, and it would go with it.


Second pass

Complete. Runs against my server's dictionary words file.

use warnings;
use strict;

my $dictionary_file = '/usr/share/dict/words';

my @pad = ( [1,2,3],[4,5,6],[7,8,9] );
my %adjacent;

for my $rows (0..2) {
   for my $cols (0..2) {
      my @allowed_columns = grep { $_ >= 0 && $_ <= 2 }
                            map { $cols + $_ }
                            (-1..1);
      my @adjnt = grep { $_ != $pad[$rows][$cols] }
                  map { @{$pad[$_]}[@allowed_columns] }
                  grep { ( $_ >= 0 ) && ( $_ <= 2 ) }
                  map { $rows + $_ }
                  (-1..1);
      $adjacent{$pad[$rows][$cols]}{$_} = 1 for @adjnt;
   }
}

my @illegal_pairs;

for my $i (2..9) {
   for my $j (2..9) {
       push @illegal_pairs, "$i$j" unless $adjacent{$i}{$j};
   }
}
my $regex = join "|", @illegal_pairs;

open( my $dict, "<", $dictionary_file )
        or die( "Couldn't open $dictionary_file: $!" );

while( <$dict> )
{
   chomp;
   next if ( m/[qz\W]/i || ! $_ );
   my $try = lc $_;

   $try =~ tr{abcdefghijklmnoprstuvwxy}
             {222333444555666777888999};

   next if $try =~ m/$regex/o;

   my $score = eval join( "+", split( "", $try ));
   print "$_\t$try\t$score\n" if ( $score > 70 );
}