aboutsummaryrefslogtreecommitdiff
path: root/challenge-064
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-06-13 13:33:32 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-06-13 13:33:32 +0100
commit8b1109f2c5e11d9b7b88e9ff188c2b3137060561 (patch)
tree80ab5c9e44858c56d86b52d364068e4b034f8183 /challenge-064
parent41ee0088c32048507dd6ad03ee94b22715a5cf9a (diff)
downloadperlweeklychallenge-club-8b1109f2c5e11d9b7b88e9ff188c2b3137060561.tar.gz
perlweeklychallenge-club-8b1109f2c5e11d9b7b88e9ff188c2b3137060561.tar.bz2
perlweeklychallenge-club-8b1109f2c5e11d9b7b88e9ff188c2b3137060561.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-064')
-rw-r--r--challenge-064/ulrich-rieke/perl/ch-1.pl103
-rw-r--r--challenge-064/ulrich-rieke/perl/ch-2.pl19
2 files changed, 122 insertions, 0 deletions
diff --git a/challenge-064/ulrich-rieke/perl/ch-1.pl b/challenge-064/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..eb597d9f9c
--- /dev/null
+++ b/challenge-064/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,103 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use Algorithm::Combinatorics qw ( variations_with_repetition ) ;
+
+sub enterArray {
+ my $columns = shift ;
+ my $rows = shift ;
+ my $rowcount = 0 ;
+ my @array ;
+ while ( $rowcount != $rows ) {
+ print "Enter $columns integers, separated by space!\n" ;
+ my $input = <STDIN> ;
+ chomp $input ;
+ while ( scalar ( split ( /\s+/ , $input ) ) != $columns ) {
+ print "Not enough columns entered! Please repeat entry!\n" ;
+ $input = <STDIN> ;
+ chomp $input ;
+ }
+ my @line = split ( /\s+/ , $input ) ;
+ push ( @array , \@line ) ;
+ $rowcount++ ;
+ }
+ return @array ;
+}
+
+#we have to create different paths through the number array.
+#we can do that by defining rightways shifts for every different row.
+#to arrive at the right lower point, the rightward shifts in every row
+#have to add up to the number of columns.
+#I numbered the columns from 0 to columns - 1.
+#by creating "variations with repetitions" with as many elements as rows
+#and selecting those that add up to the number of columns I get
+#combinations of shifts per row that lead to the right lower corner
+sub createPaths {
+ my $columns = shift ;
+ my $rows = shift ;
+ my @onerow = (0..$columns - 1) ;
+ my @possibleCombis ;
+ my $iter = variations_with_repetition( \@onerow, $rows ) ;
+ while ( my $p = $iter->next ) {
+ if ( sumUpArray( $p ) == $columns - 1 ) {
+ push( @possibleCombis, $p ) ;
+ }
+ }
+ return @possibleCombis ;
+}
+
+sub sumUpArray {
+ my $array = shift ;
+ my $sum = 0 ;
+ for my $i ( @{$array} ) {
+ $sum += $i ;
+ }
+ return $sum ;
+}
+
+#the number array and the list of shift combinations per row are taken
+#in as parameters. We then go through the array and pick up numbers on
+#the way
+sub findPath {
+ my $numbers = shift ;
+ my $path = shift ;
+ my @nums ;
+ my @array = @{$numbers} ;
+ my $currentcolumn = 0 ;
+ my $currentrow = 0 ;
+ while ( @{$path} ) {
+ my $howmany = shift @{$path} ;
+ if ( $howmany > 0 ) {
+ push( @nums , @{$array[ $currentrow ] }[ $currentcolumn..
+ $currentcolumn + $howmany]) ;
+ $currentcolumn += $howmany ;
+ }
+ else {
+ push( @nums , ${$array[ $currentrow ]}[$currentcolumn] ) ;
+ }
+ $currentrow++ ;
+ }
+ return @nums ;
+}
+
+print "How many columns ?\n" ;
+my $columns = <STDIN> ;
+chomp $columns ;
+print "How many rows ?\n" ;
+my $rows = <STDIN> ;
+chomp $rows ;
+my @numbers = enterArray( $columns , $rows ) ;
+my @combis = createPaths( $columns, $rows ) ;
+my $minsum = 1000000 ;
+my @minpath ;
+foreach my $c ( @combis ) {
+ my @nums = findPath( \@numbers , $c ) ;
+ my $sum = sumUpArray( \@nums ) ;
+ if ( $sum < $minsum ) {
+ $minsum = $sum ;
+ @minpath = @nums ;
+ }
+}
+print "$minsum(" ;
+print join ( '->' , @minpath ) ;
+print ")\n" ;
diff --git a/challenge-064/ulrich-rieke/perl/ch-2.pl b/challenge-064/ulrich-rieke/perl/ch-2.pl
new file mode 100644
index 0000000000..53977cdd89
--- /dev/null
+++ b/challenge-064/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+
+my $S = $ARGV[0] ;
+my @W = ( "weekly" , "challenge" , "perl" ) ;
+my @found ;
+for my $word ( @W ) {
+ if ( $S =~ /$word/ ) {
+ push( @found , $word ) ;
+ }
+}
+if ( @found ) {
+ print join( ',' , @found ) ;
+ print "\n" ;
+}
+else {
+ print "0 as none matching word found.\n" ;
+}