diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-01-24 21:12:30 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-01-24 21:12:30 +0000 |
| commit | bac75dac372ec9302702a9afaa6608fc02b35207 (patch) | |
| tree | 17ea890f86e405e5d1839d156b32bf7364cacc16 /challenge-044 | |
| parent | 82a0002c9e5239704507a807e947c088a92e87b3 (diff) | |
| download | perlweeklychallenge-club-bac75dac372ec9302702a9afaa6608fc02b35207.tar.gz perlweeklychallenge-club-bac75dac372ec9302702a9afaa6608fc02b35207.tar.bz2 perlweeklychallenge-club-bac75dac372ec9302702a9afaa6608fc02b35207.zip | |
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-044')
| -rw-r--r-- | challenge-044/ulrich-rieke/raku/ch-1.p6 | 204 | ||||
| -rw-r--r-- | challenge-044/ulrich-rieke/raku/ch-2.p6 | 20 |
2 files changed, 224 insertions, 0 deletions
diff --git a/challenge-044/ulrich-rieke/raku/ch-1.p6 b/challenge-044/ulrich-rieke/raku/ch-1.p6 new file mode 100644 index 0000000000..e80d16b88b --- /dev/null +++ b/challenge-044/ulrich-rieke/raku/ch-1.p6 @@ -0,0 +1,204 @@ +use v6 ; + +#in order to create the analogon of list comprehensions, I had to create +#various functions to take account of differing numbers of digit +#combinations. Unfortunately, this led to code repetition. +#to print out the results of the gather-take loops turned out to be more +#difficult than originally envisaged +#somehow you have to circumvent the fact the immutability of resulting +#strings + +sub testSubSequences_8( @numbers ) { + my @sources ; + for (0..7 ) -> $i { + @sources.push( @numbers[ $i ] , -@numbers[ $i ] ) ; + } + my @fittingnumbers = gather { + for @sources[0,1] -> $n0 { + for @sources[2,3] -> $n1 { + for @sources[4,5] -> $n2 { + for @sources[6,7] -> $n3 { + for @sources[8,9] -> $n4 { + for @sources[10,11] -> $n5 { + for @sources[12,13] -> $n6 { + for @sources[14,15] -> $n7 { + if ( $n0 + $n1 + $n2 + $n3 + $n4 + $n5 + $n6 + $n7 ) == 100 { + take $n0 , $n1 , $n2 , $n3 , $n4 , $n5 , $n6 , $n7 ; + } + } + } + } + } + } + } + } + } + } + if @fittingnumbers { + for @fittingnumbers -> @array { + my $str = join '+' , @array ; + $str ~~ s:g/\+\-/\-/ ; + say $str ; + } + } +} + +sub testSubSequences_7( @numbers ) { + my @sources ; + for (0..6 ) -> $i { + @sources.push( @numbers[ $i ] , -@numbers[ $i ] ) ; + } + my @fittingnumbers = gather { + for @sources[0, 1] -> $n0 { + for @sources[2 , 3] -> $n1 { + for @sources[4 , 5] -> $n2 { + for @sources[6 , 7] -> $n3 { + for @sources[8 , 9] -> $n4 { + for @sources[10 , 11 ] -> $n5 { + for @sources[12 , 13] -> $n6 { + if ( $n0 + $n1 + $n2 + $n3 + $n4 + $n5 + $n6 ) == 100 { + take $n0 , $n1 , $n2 , $n3 , $n4 , $n5 , $n6 ; + } + } + } + } + } + } + } + } + } + if @fittingnumbers { + for @fittingnumbers -> @array { + my $str = join '+', @array ; + $str ~~ s:g/\+\-/\-/ ; + say $str ; + } + } +} + +sub testSubSequences_6( @numbers ) { + my @sources ; + for (0..5 ) -> $i { + @sources.push( @numbers[ $i ] , -@numbers[ $i ] ) ; + } + my @fittingnumbers = gather { + for @sources[0,1] -> $n0 { + for @sources[2 , 3] -> $n1 { + for @sources[4 , 5] -> $n2 { + for @sources[6 , 7] -> $n3 { + for @sources[8 , 9] -> $n4 { + for @sources[10, 11] -> $n5 { + if ( $n0 + $n1 + $n2 + $n3 + $n4 + $n5 ) == 100 { + take $n0 , $n1 , $n2 , $n3 , $n4 , $n5 ; + } + } + } + } + } + } + } + } + if @fittingnumbers { + for @fittingnumbers -> @array { + my $str = join '+' , @array ; + $str ~~ s:g/\+\-/\-/ ; + say $str ; + } + } +} + +sub testSubSequences_5( @numbers ) { + my @sources ; + for (0..4 ) -> $i { + @sources.push( @numbers[ $i ] , -@numbers[ $i ] ) ; + } + my @fittingnumbers = gather { + for @sources[0, 1] -> $n0 { + for @sources[2 , 3] -> $n1 { + for @sources[4 , 5] -> $n2 { + for @sources[6 , 7] -> $n3 { + for @sources[8 , 9] -> $n4 { + if ( $n0 + $n1 + $n2 + $n3 + $n4 ) == 100 { + take $n0 , $n1 , $n2 , $n3 , $n4 ; + } + } + } + } + } + } + } + if @fittingnumbers { + for @fittingnumbers -> @array { + my $str = join '+' , @array ; + $str ~~ s:g/\+\-/\-/ ; + s ay $str ; + } + } +} + +sub testSubSequences_4( @numbers ) { + my @sources ; + for (0..3 ) -> $i { + @sources.push( @numbers[ $i ] , -@numbers[ $i ] ) ; + } + my @fittingnumbers = gather { + for @sources[0, 1] -> $n0 { + for @sources[2 , 3] -> $n1 { + for @sources[4, 5] -> $n2 { + for @sources[6, 7] -> $n3 { + if ( $n0 + $n1 + $n2 + $n3 ) == 100 { + take $n0 , $n1 , $n2 , $n3 ; + } + } + } + } + } + } + if @fittingnumbers { + for @fittingnumbers -> @array { + my $str = join '+' , @array ; + $str ~~ s:g/\+\-/\-/ ; + say $str ; + } + } +} + +#function to find the substrings and numbers according to length sequences +sub findNumbers( Str $digits , $seq ) { + my @numbers ; + my $current = 0 ; + for $seq.Array -> $i { + @numbers.push( $digits.substr( $current , $i ).Int) ; + $current += $i ; + } + return @numbers ; +} + + +my $digits = "123456789" ; +#a combination of 3 three-digit-numbers will not result in 100 as a sum! +#therefore, the likely combinations in length are : ( all summing up to 9) +my @likelyCombos = ( (1 , 2 , 2 , 2 , 2 ) , (1 , 1 , 1 , 1 , 1 , 2 , 2 ), + (3 , 2 , 2 , 2 ) , ( 1 , 2 , 3 , 2 , 1 ) , ( 3 , 3 , 2 , 1 ) , + ( 3 , 3 , 1 , 1 , 1 ) , (1 , 1 , 1 , 1 , 1 , 1, 1, 2) ) ; +#we now permutate these combinations and form substrings , that is numbers +#accordingly, preserving the order of digits + +#the dispatcher helps to select the right list comprehension depending on +#length of the @numbers array! +my %dispatcher = + 4 => &testSubSequences_4 , + 5 => &testSubSequences_5 , + 6 => &testSubSequences_6 , + 7 => &testSubSequences_7 , + 8 => &testSubSequences_8 ; + +say "+ and - can be inserted in the following ways:" ; +for @likelyCombos -> $combo { + my $selectedOp = %dispatcher{ $combo.elems } ; + my @permus = $combo.permutations.unique(:with(&[eqv])) ; + for @permus -> $permutation { + my @numbers = findNumbers( $digits , $permutation ) ; + $selectedOp( @numbers ) ; + } +} diff --git a/challenge-044/ulrich-rieke/raku/ch-2.p6 b/challenge-044/ulrich-rieke/raku/ch-2.p6 new file mode 100644 index 0000000000..86225bbbc1 --- /dev/null +++ b/challenge-044/ulrich-rieke/raku/ch-2.p6 @@ -0,0 +1,20 @@ +use v6 ; + +sub doublingSteps( Int $n is copy ) { + my $count = 0 ; + while ( $n < 200 ) { + $count++ ; + $n *= 2 ; + } + return ( $n div= 2 , --$count ) ; +} + +#we assume that we go to a starting point in steps of 1 , then +#keep doubling and then move forward to 200 in steps of 1 +sub computeSteps( Int $n is copy --> Int ) { + my @result = doublingSteps( $n ) ; + return ( $n - 1 + @result[1] + 200 - @result[0] ) ; +} + +my $moves = (1..100).map( { computeSteps( $_ ) } ).sort( {$^a <=> $^b})[0] ; +say "The minimum number of moves is $moves!" ; |
