6 print "5x5 matrix in one line: " unless @ARGV;
7 my $matrix = shift || <>;
9 $matrix ||= "abcdefghijklmnopqrstuvwxy";
10 my @matrix = [ ('_') x
7 ];
11 push @matrix, [ '_', (split //, substr $matrix, 0, 5, ''), '_' ] while $matrix;
12 push @matrix, [ ('_') x
7 ];
21 $matrix[$y + $dy][$x + $dx] eq '_' and next;
22 push @
{ $adj[$y][$x] }, { y
=> $y + $dy, x
=> $x + $dx };
29 my ($y, $x, $todo, $had) = @_;
30 my $r = $matrix[$y][$x] or die "y=$y,x=$x is empty (@_)";
32 my %had = $had ?
%$had : ("$y/$x" => 1); # copy
35 $had{"$_->{y}/$_->{x}"}++
37 : build_re
($_->{y
}, $_->{x
}, $todo, \
%had)
43 ?
$r . (@next == 1 ?
"@next?" : '[' . join('', @next) . ']?')
44 : $r . '(?:' . join('|', @next) . ')' . ($todo < 4 ?
'?' : '');
51 push @re, build_re
$y, $x, 6;
55 my $re = join '|', @re;
56 $re = "^(?:$re)\\z"; # Don't compile yet - once is enough
59 a
=> 1, b
=> 3, c
=> 3, d
=> 2, e
=> 1, f
=> 4, g
=> 2, h
=> 4, i
=> 1,
60 j
=> 8, k
=> 5, l
=> 1, m
=> 3, n
=> 1, o
=> 1, p
=> 3, q
=>10, r
=> 1,
61 s
=> 1, t
=> 1, u
=> 1, v
=> 4, w
=> 4, x
=> 8, y
=> 4, z
=>10
63 $_ *= 10 for values %scores;
66 open my $fh, '/usr/share/dict/american-english' or die $!;
68 substr(join('', @
{ $matrix[1] }), 1, 5) =~ /$re/ or die; # Precompile
71 next if tr/a-z//c; # Regex would destroy the compiled one
72 // and push @matches, [ $_, sum
map $scores{$_}, split // ];
73 # Re-use precompiled regex
77 $b->[1] <=> $a->[1] # high score .. low score
78 || length $a->[0] <=> length $b->[0] # short .. long
79 || $a->[0] cmp $b->[1] # a .. z
82 printf "MATRIX IS WORTH %d POINTS\n", sum
map $_->[1], @sorted;
83 printf "%3d %s\n", $_->[1], $_->[0] for @sorted;