[CursorBase] split out sys_find_module from sys_load_module
[pugs.git] / examples / matrix.pl
blobb5a0d326e5b0dd778b3e1cc624953414bcf320d8
1 use v6;
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 ];
11 my @adj;
12 for 1..5 -> $y {
13 for 1..5 -> $x {
14 for -1..1 -> $dx {
15 for -1..1 -> $dy {
16 $dy or $dx or next;
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) {
26 %had{"$y/$x"} = 1;
27 my $r = @matrix[$y][$x] or die "y=$y,x=$x is empty";
28 --$todo or return $r;
30 my @next; #= gather {
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;
36 #} or return $r;
37 @next or return $r;
39 return $todo == 1
40 ?? "$r\<[{ @next.join('') }]>?"
41 !! "$r\[{ @next.join('|') }]{ $todo < 4 ?? '?' !! '' }";
43 my @re;
44 #say build_re 1, 1, 5;
45 #say "done";
46 #exit;
48 #my $re = #rule {
49 # ^ [ <$(
50 for 1..5 -> $y {
51 for 1..5 -> $x {
52 push @re, build_re $y, $x, 6;
55 # }
56 # )> ] $
57 #};
59 my $re = join('|', @re);
61 =foo
63 my %scores = (
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;
70 gather {
71 for slurp '/usr/share/dict/words' :chomp orelse die {
72 next if /<-[a-z]>/;
73 /$re/ and take { word => $_, score => [+](%scores{ .letters }) };
76 ==> sort [ { -.<score> }, { .<word>.length }, { .<word> } ];
77 ==> my @words;
79 say sprintf 'MATRIX IS WORTH %d POINTS' <== [+] @words>>.[0];
80 say sprintf '%3d %s' <== $_[1], $_[0] for @words;