3 print "5x5 matrix in one line: " unless @
*ARGS
;
4 my $matrix = @
*ARGS
[0] || get
();
5 $matrix ||= "abcdefghijklmnopqrstuvwxy";
7 $matrix.chars
== 25 or die "Matrix length MUST be 25 characters.\n";
8 my @matrix = [ '_' xx
7 ];
9 push @matrix, [ '_', (split "", substr $matrix, 0, 5, ''), '_' ] while $matrix;
10 push @matrix, [ '_' xx
7 ];
17 @matrix[$y + $dy][$x + $dx] eq '_' and next;
18 push @adj[$y][$x], { x
=> ($x + $dx), y
=> ($y + $dy) };
25 sub build_re
($y, $x, $todo is copy
, %had? is copy
) {
27 my $r = @matrix[$y][$x] or die "y=$y,x=$x is empty";
31 for @adj[$y][$x] -> $adj {
32 %had{"$adj<y>/$adj<x>"}++ and next;
33 #take build_re $adj<y>, $adj<x>, $todo, %had;
34 push @next, build_re
$adj<y
>, $adj<x
>, $todo, %had;
40 ??
"$r\<[{ @next.join('') }]>?"
41 !! "$r\[{ @next.join('|') }]{ $todo < 4 ?? '?' !! '' }";
44 #say build_re 1, 1, 5;
52 push @re, build_re
$y, $x, 6;
59 my $re = join('|', @re);
64 a
=> 1, b
=> 3, c
=> 3, d
=> 2, e
=> 1, f
=> 4, g
=> 2, h
=> 4, i
=> 1,
65 j
=> 8, k
=> 5, l
=> 1, m
=> 3, n
=> 1, o
=> 1, p
=> 3, q
=>10, r
=> 1,
66 s
=> 1, t
=> 1, u
=> 1, v
=> 4, w
=> 4, x
=> 8, y
=> 4, z
=>10
68 %scores.values >>*=>> 10;
71 for slurp
'/usr/share/dict/words' :chomp orelse
die {
73 /$re/ and take
{ word
=> $_, score
=> [+](%scores{ .letters
}) };
76 ==> sort [ { -.<score
> }, { .<word
>.length }, { .<word
> } ];
79 say sprintf 'MATRIX IS WORTH %d POINTS' <== [+] @words>>.[0];
80 say sprintf '%3d %s' <== $_[1], $_[0] for @words;