aboutsummaryrefslogtreecommitdiff
path: root/challenge-082/athanasius/raku/ch-2.raku
blob: b33232fd81514fea04e1a4edd4dc78551059a03b (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use v6d;

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

Perl Weekly Challenge 082
=========================

Task #2
-------
*Interleave String*

Submitted by: Mohammad S Anwar

You are given 3 strings; $A, $B and $C.

Write a script to check if $C is created by interleave $A and $B.

Print 1 if check is success otherwise 0.

Example 1:

 Input:
     $A = "XY"
     $B = "X"
     $C = "XXY"

 Output: 1

EXPLANATION

 "X" (from $B) + "XY" (from $A) = $C

Example 2:

 Input:
     $A = "XXY"
     $B = "XXZ"
     $C = "XXXXZY"

 Output: 1

EXPLANATION

 "XX" (from $A) + "XXZ" (from $B) + "Y" (from $A) = $C

Example 3:

 Input:
     $A = "YX"
     $B = "X"
     $C = "XXY"

 Output: 0

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

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

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

Notes:

1. It is assumed that strings $A and $B must be fully consumed by the inter-
   leaving process that creates string $C

2. The interleaving check is performed by sub interleave, which is recursive

3. Where more than one solution is possible, only the first will be given in
   the explanation: namely, the solution found by taking letters from $A before
   $B where both are valid options

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

enum < A B C >;

my Bool constant $VERBOSE = True;

#------------------------------------------------------------------------------
BEGIN
#------------------------------------------------------------------------------
{
    "\nChallenge 082, Task #2: Interleave String (Raku)\n".put;
}

##=============================================================================
sub MAIN
(
    Str:D $A,      #= First  string
    Str:D $B,      #= Second string
    Str:D $C,      #= Third  string: can it be created by interleaving A and B?
)
##=============================================================================
{
    "Input:"\           .put;
    qq[    \$A = "$A"]\ .put;
    qq[    \$B = "$B"]\ .put;
    qq[    \$C = "$C"\n].put;

    my Str  @sequence;
    my Bool $is-interleaved = $C.chars == $A.chars + $B.chars   ??
                              interleave($A, $B, $C, @sequence) !! False;

    "Output: %d\n".printf: $is-interleaved ?? 1 !! 0;

    $is-interleaved && explain($A, $B, $C, @sequence) if $VERBOSE;
}

#------------------------------------------------------------------------------
sub interleave( Str:D $A, Str:D $B, Str:D $C, Array:D[Str:D] $seq --> Bool:D )
#------------------------------------------------------------------------------
{
    my Bool $success = False;
    my UInt @length  = ($A, $B, $C).map: { .chars };

    if    @length[A] == 0                                         # Base case 1
    {
        if $B eq $C
        {
            $success = True;
            $seq.push: 'B'              if $VERBOSE;
        }
    }
    elsif @length[B] == 0                                         # Base case 2
    {
        if $A eq $C
        {
            $success = True;
            $seq.push: 'A'              if $VERBOSE;
        }
    }
    else
    {
        my Str $A0 = $A.substr: 0, 1;
        my Str $AA = $A.substr: 1;
        my Str $C0 = $C.substr: 0, 1;
        my Str $CC = $C.substr: 1;

        if $C0 eq $A0                                        # Recursive case 1
        {
            $seq.push: 'A'              if $VERBOSE;
            $success = interleave($AA, $B, $CC, $seq);
            $success or $seq.pop        if $VERBOSE;
        }

        unless $success
        {
            my $B0 = $B.substr: 0, 1;
            my $BB = $B.substr: 1;

            if $C0 eq $B0                                    # Recursive case 2
            {
                $seq.push: 'B'          if $VERBOSE;
                $success = interleave($A, $BB, $CC, $seq);
                $success or $seq.pop if $VERBOSE;
            }
        }
    }

    return $success;
}

#------------------------------------------------------------------------------
sub explain( Str:D $A, Str:D $B, Str:D $C, Array:D[Str:D] $seq )
#------------------------------------------------------------------------------
{
    my (UInt $ai, UInt $bi) = 0, 0;
    my (Str  @A,  Str  @B);

    for 0 .. $seq.end - 1 -> UInt $i
    {
        if $seq[$i] eq 'A'
        {
            @A.push: $A.substr: $ai++, 1;
            @B.push: ' ';
        }
        else
        {
            push @A, ' ';
            push @B, $B.substr: $bi++, 1;
        }
    }

    if $seq[*-1] eq 'A'
    {
        @A.push: $A.substr: $ai;
    }
    else
    {
        @B.push: $B.substr: $bi;
    }

    "\nEXPLANATION"\ .put;
    "    \$A =  %s\n".printf: @A.join: '';
    "    \$B =  %s\n".printf: @B.join: '';
    "    \$C =  $C"\ .put;
}

#------------------------------------------------------------------------------
sub USAGE()
#------------------------------------------------------------------------------
{
    my Str $usage = $*USAGE;

    $usage ~~ s/ ($*PROGRAM-NAME) /raku $0/;
    $usage.put;
}

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