aboutsummaryrefslogtreecommitdiff
path: root/challenge-117
diff options
context:
space:
mode:
authorwanderdoc <wanderdoc@googlemail.com>2021-06-20 16:03:31 +0200
committerwanderdoc <wanderdoc@googlemail.com>2021-06-20 16:03:31 +0200
commit2fd2e3ec9125ec9138eefa2d0ada5bd1bc238e2c (patch)
treeae184a26e48f2a2b91464c12721afc5aa098b420 /challenge-117
parent255cff712c7fae9d2d5857255d2be789d77ea99c (diff)
downloadperlweeklychallenge-club-2fd2e3ec9125ec9138eefa2d0ada5bd1bc238e2c.tar.gz
perlweeklychallenge-club-2fd2e3ec9125ec9138eefa2d0ada5bd1bc238e2c.tar.bz2
perlweeklychallenge-club-2fd2e3ec9125ec9138eefa2d0ada5bd1bc238e2c.zip
Solutions to challenge-117
Diffstat (limited to 'challenge-117')
-rw-r--r--challenge-117/wanderdoc/perl/ch-1.pl57
-rw-r--r--challenge-117/wanderdoc/perl/ch-2.pl91
2 files changed, 148 insertions, 0 deletions
diff --git a/challenge-117/wanderdoc/perl/ch-1.pl b/challenge-117/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..6eb908217e
--- /dev/null
+++ b/challenge-117/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,57 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given text file with rows numbered 1-15 in random order but there is a catch one row in missing in the file.
+11, Line Eleven
+1, Line one
+9, Line Nine
+13, Line Thirteen
+2, Line two
+6, Line Six
+8, Line Eight
+10, Line Ten
+7, Line Seven
+4, Line Four
+14, Line Fourteen
+3, Line three
+15, Line Fifteen
+5, Line Five
+Write a script to find the missing row number.
+=cut
+
+
+my %text;
+
+while ( my $line = <DATA> )
+{
+ chomp $line;
+ my ($nr, $row) = split(/\s*,\s*/, $line);
+ $text{$nr} = undef;
+}
+
+my ($min, $max) = (sort {$a <=> $b} keys %text)[0, -1];
+my %check;
+
+$check{$_} = undef for $min .. $max;
+delete @check{ keys %text };
+
+
+print "$_$/" for keys %check; # 12
+
+__END__
+11, Line Eleven
+1, Line one
+9, Line Nine
+13, Line Thirteen
+2, Line two
+6, Line Six
+8, Line Eight
+10, Line Ten
+7, Line Seven
+4, Line Four
+14, Line Fourteen
+3, Line three
+15, Line Fifteen
+5, Line Five \ No newline at end of file
diff --git a/challenge-117/wanderdoc/perl/ch-2.pl b/challenge-117/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..63e7ee52ea
--- /dev/null
+++ b/challenge-117/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,91 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+
+=prompt
+You are given size of a triangle. Write a script to find all possible paths from top to the bottom right corner.
+In each step, we can either move horizontally to the right (H), or move downwards to the left (L) or right (R).
+
+BONUS: Try if it can handle triangle of size 10 or 20.
+
+Example 1:
+
+Input: $N = 2
+
+ S
+ / \
+ / _ \
+ /\ /\
+ /__\ /__\ E
+
+Output: RR, LHR, LHLH, LLHH, RLH, LRH
+
+
+Example 2:
+
+Input: $N = 1
+
+ S
+ / \
+ / _ \ E
+
+Output: R, LH
+
+=cut
+
+
+my $N = shift or die "Size of triangle? (Warning: Output takes long by size over 10)$/"; # my $N = 10; # 1037718
+my @triangle;
+my $counter = 0;
+for my $i ( 0 .. $N )
+{
+
+ my @row = map $counter++, 0 .. $i;
+
+ push @triangle, [@row];
+}
+
+$counter--; # !
+my %ways = (R => [1, 1], L => [1, 0], H =>[0, 1]);
+
+# my @SOLS; # A way to save output (not recommended by size over 10).
+
+
+sub find_ways
+{
+ my ($triangle, $x, $y, $solution) = @_;
+
+
+ if ($triangle->[$x][$y] == $counter )
+ {
+ my $s = [@$solution];
+
+ print join('-', @$s), $/; # push @SOLS, $s;
+ }
+
+
+
+ for my $w ( keys %ways )
+ {
+ my $next_x = $x + $ways{$w}->[0];
+ my $next_y = $y + $ways{$w}->[1];
+
+ if ( exists $triangle->[$next_x][$next_y] )
+ {
+ push @$solution, $w;
+
+ if (find_ways($triangle, $next_x, $next_y, $solution))
+ {
+ return 1;
+ }
+ else
+ {
+ pop @$solution;
+ }
+
+ }
+ }
+}
+
+find_ways([@triangle], 0, 0, []); \ No newline at end of file