aboutsummaryrefslogtreecommitdiff
path: root/challenge-076/markus-holzer/raku/ch-2.explained.raku
blob: 061e2a79862275771ce272958521f4364c278f90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
unit sub MAIN($words-file, $board-file);

my @words = $words-file.IO.lines;
say "Looking for words:";
dd @words;

my @chars = $board-file.IO.slurp.comb( /\H/ );
say "Grid:\n", @chars.join;
say "Searching these characters:";
dd @chars;

my $width = @chars.first( "\n", :k ) + 1;
say "Grid has a width of {$width}";

my $rotated-chars = rotated-chars;
say "Data after rotation";
dd $rotated-chars;

my $search-text = $rotated-chars.flat.join;
say "Final text to search:";
dd $search-text;

say "Results:";
.Str.say for words-in $search-text;

sub words-in( $text ) {
    $text ~ "\n" ~ $text.flip ~~ m:ex:i/@words/ }

sub rotated-chars {
    eager                   # just so the output is in the right order
    @chars.batch( $width ), # horizontal lines
    | (
        (0,0),              # vertical lines
        (0,1),              # left to right diagonal
        (1,0)               # right to left diagonal
    ).map: &rotate-chars }

sub rotate-chars( @offsets ) {
    say "Rotate the grid with offsets";
    dd @offsets;

    eager
    (^$width).map: -> $start {
        my $first = $start + @offsets[0];
        my $next  = $start + $width + @offsets[1];
        say "Line {$start}, Indices: {$first}, {$next} ... *";
        | @chars[  $first, $next ... * ], "\n";
    }
}