aboutsummaryrefslogtreecommitdiff
path: root/challenge-053/athanasius/raku/ch-1.p6
blob: fd5c7e703d0c4f8608346437735416f3a04a5752 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use v6;

################################################################################
=begin comment

Perl Weekly Challenge 053
=========================

Task #1
*Rotate Matrix*

Write a script to rotate the followin(g) matrix by given *90/180/270 degrees*
clockwise.

  ( 1, 2, 3 )
  ( 4, 5, 6 )
  ( 7, 8, 9 )

For example, if you rotate by *90 degrees* then expected result should be like
below

  ( 7, 4, 1 )
  ( 8, 5, 2 )
  ( 9, 6, 3 )

=end comment
################################################################################

#--------------------------------------#
# Copyright © 2020 PerlMonk Athanasius #
#--------------------------------------#

my constant @MATRIX    = (1, 2, 3), (4, 5, 6), (7, 8, 9);
my constant %ROTATIONS =  90 => (
                                    ( (2, 0), (1, 0), (0, 0) ),
                                    ( (2, 1), (1, 1), (0, 1) ),
                                    ( (2, 2), (1, 2), (0, 2) ),
                                ),
                         180 => (
                                    ( (2, 2), (2, 1), (2, 0) ),
                                    ( (1, 2), (1, 1), (1, 0) ),
                                    ( (0, 2), (0, 1), (0, 0) ),
                                ),
                         270 => (
                                    ( (0, 2), (1, 2), (2, 2) ),
                                    ( (0, 1), (1, 1), (2, 1) ),
                                    ( (0, 0), (1, 0), (2, 0) ),
                                );

#-------------------------------------------------------------------------------
BEGIN ''.put;
#-------------------------------------------------------------------------------

#===============================================================================
sub MAIN()
#===============================================================================
{
    "Challenge 053, Task #1: Rotate Matrix (Raku)\n".put;
    format-matrix('Original matrix', @MATRIX).put;

    my @rotation;

    for %ROTATIONS.keys.sort: { $^a <=> $^b } -> Str $degrees
    {
        @rotation = rotate($degrees, @MATRIX);

        format-matrix("Rotated $degrees degrees", @rotation).put;
    }

    # 270° rotated a further 90° --> 360° = 0° (the original matrix)

    format-matrix('Rotated 360 degrees', rotate('90', @rotation)).print;
}

#-------------------------------------------------------------------------------
sub format-matrix(Str:D $title, @matrix --> Str)
#-------------------------------------------------------------------------------
{
    my Str $string  =  "%s:\n".sprintf: $title;
           $string ~= "(%s)\n".sprintf: @matrix[$_].join: ', ' for 0 .. 2;

    return $string;
}

#-------------------------------------------------------------------------------
sub rotate(Str:D $degrees, @old-matrix --> Array)
#-------------------------------------------------------------------------------
{
    %ROTATIONS{ $degrees }:exists
        or die "ERROR: Rotation of $degrees degrees is not supported\n";

    my @new-matrix;

    for 0 .. 2 -> UInt $row-new
    {
        for 0 .. 2 -> UInt $col-new
        {
            my UInt @old = %ROTATIONS{ $degrees }[$row-new][$col-new];

            @new-matrix[$row-new][$col-new] = @old-matrix[ @old[0] ][ @old[1] ];
        }
    }

    return @new-matrix;
}

################################################################################