aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-344/mahnkong/perl/ch-1.pl14
-rw-r--r--challenge-344/mahnkong/perl/ch-2.pl41
2 files changed, 55 insertions, 0 deletions
diff --git a/challenge-344/mahnkong/perl/ch-1.pl b/challenge-344/mahnkong/perl/ch-1.pl
new file mode 100644
index 0000000000..9f8f21e5ff
--- /dev/null
+++ b/challenge-344/mahnkong/perl/ch-1.pl
@@ -0,0 +1,14 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run($ints, $x) {
+ return [ split("", join('', @$ints) + $x) ];
+}
+
+is_deeply(run([1,2,3,4], 12), [1,2,4,6], "Example 1");
+is_deeply(run([2,7,4], 181), [4,5,5], "Example 2");
+is_deeply(run([9,9,9], 1), [1,0,0,0], "Example 3");
+is_deeply(run([1,0,0,0,0], 9999), [1,9,9,9,9], "Example 4");
+is_deeply(run([0], 1000), [1,0,0,0], "Example 5");
diff --git a/challenge-344/mahnkong/perl/ch-2.pl b/challenge-344/mahnkong/perl/ch-2.pl
new file mode 100644
index 0000000000..1a4043198f
--- /dev/null
+++ b/challenge-344/mahnkong/perl/ch-2.pl
@@ -0,0 +1,41 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub permute(@items) {
+ return ([[ @items ]]) if @items <= 1;
+
+ my @result;
+
+ for my $i (0 .. $#items) {
+ my @rest = @items[0 .. $i-1, $i+1 .. $#items];
+ for my $p (@{ permute(@rest) }) {
+ push @result, [ $items[$i], @$p ];
+ }
+ }
+
+ return \@result;
+}
+
+sub run($source, $target) {
+ my @source_strings = map { join ("", @{$_}) } @$source;
+ my $target_string = join("", @$target);
+
+ my $permutations = permute(@source_strings);
+ foreach my $permutation (@$permutations) {
+ my $work = $target_string;
+ foreach my $s (@$permutation) {
+ $work =~ s/$s//;
+ }
+ return 1 if length($work) == 0;
+ }
+ return 0;
+}
+
+is(run([[2,3], [1], [4]], [1, 2, 3, 4]), 1, "Example 1");
+is(run([[1,3], [2,4]], [1, 2, 3, 4]), 0, "Example 2");
+is(run([[9,1], [5,8], [2]], [5, 8, 2, 9, 1]), 1, "Example 3");
+is(run([[1], [3]], [1, 2, 3]), 0, "Example 4");
+is(run([[7,4,6]], [7, 4, 6]), 1, "Example 5");
+is(run([[1,2,3],[4],[5,6],[1,2],[3,4,5]], [1,2,3,4,5,1,2,3,4,5,6]), 1, "Example from \@choroba");