aboutsummaryrefslogtreecommitdiff
path: root/challenge-082/pete-houston/perl/ch-2.pl
blob: 5b80c7bd2247daddf8d7da91e5eac71b3aa410b5 (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
#!/usr/bin/env perl 
#===============================================================================
#
#         FILE: 8202.pl
#
#        USAGE: ./8202.pl substr1 substr2 str
#
#  DESCRIPTION: Report if str can be made by "interleaving" the substrs
#
#        NOTES: Prints 1 if the string can be made, otherwise 0
#       AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
# ORGANIZATION: Openstrike
#      VERSION: 1.0
#      CREATED: 17/10/20
#===============================================================================

use strict;
use warnings;

my @sub = @ARGV;
my $str = pop @sub;

# No point going further if this basic test fails
nope () if length ($str) ne length ($sub[0] . $sub[1]);

# Try tucking first substring into second, then vice-versa
for my $i (0, 1) {
	my $j = 1 - $i;
	my $jpos = index ($str, $sub[$j]);
	while ($jpos > -1) {
		# $sub[$j] is contained within $str, so strip it and see if we
		# are left with $sub[$i]
		my $copy = $str;
		substr ($copy, $jpos, length ($sub[$j]), '');
		yep () if $copy eq $sub[$i];
		# No joy, so keep looking
		$jpos = index ($str, $sub[$j], $jpos + 1);
	}
}
nope ();

sub yep {
	print "1\n";
	exit;
}

sub nope {
	print "0\n";
	exit;
}