about summary refs log tree commit diff stats
path: root/lib/Octans/Puzzle.rakumod
blob: 96a4870663078cb96da3cf29424fb0af37def1f9 (plain) (blame)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
unit module Octans::Puzzle;

use WWW;

# get-puzzle returns the @puzzle along with it's @gray-squares.
sub get-puzzle (
    Str $path,

    # @puzzle will hold the puzzle grid.
    @puzzle,

    # @gray-squares will hold the position of gray squares. Algot
    # marks them with an asterisk ("*") after the character.
    @gray-squares
) is export {
    # @raw_puzzle will hold the puzzle before parsing.
    my @raw-puzzle;

    # Read the puzzle from file if it exists.
    if $path.IO.f {
        @raw-puzzle = $path.IO.lines.words;
    } else {
        # $url will hold the url that we'll call to get the toot data.
        my Str $url;

        # User can pass 2 types of links, either it will be the one
        # when they view it from their local instance or the one they
        # get from Algot's profile. We set $url from it.
        if $path.match("web/statuses") -> $match {
            $url = $match.replace-with("api/v1/statuses");
        } else {
            $url = "https://mastodon.art/api/v1/statuses/" ~ $path.split("/")[*-1];
        }

        # jget just get's the url & decodes the json. We access the
        # description field of 1st media attachment.
        if (jget($url)<media_attachments>[0]<description> ~~

            # This regex gets the puzzle in $match.
            / [[(\w [\*]?) \s*] ** 4] ** 4 $/) -> $match {

            @raw-puzzle = $match[0];
        }
    }
    parse-puzzle(@raw-puzzle, @puzzle, @gray-squares);
}

# parse-puzzle parses the puzzle from @raw-puzzle. It's assumed to be
# a 4x4 grid.
sub parse-puzzle (
    @raw-puzzle, @puzzle, @gray-squares
) is export {
    # @gray-squares should be empty.
    @gray-squares = ();

    # We have each character of the puzzle stored in @raw-puzzle.
    for 0 .. 3 -> $y {
        for 0 .. 3 -> $x {
            with @raw-puzzle[($y * 4) + $x].Str.lc -> $char {

                # If it ends with an asterisk then we push the
                # position to @gray-squares.
                if $char.ends-with("*") {
                    @puzzle[$y][$x] = $char.comb[0];
                    push @gray-squares, [$y, $x];
                } else {
                    @puzzle[$y][$x] = $char;
                }
            }
        }
    }
}