diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-08-24 14:30:59 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-24 14:30:59 +0100 |
| commit | 845c85cf263785b2a9c520743645f83ecef4b9a5 (patch) | |
| tree | 054de2950f3370430aa4f57c7dbfe529050dc53b | |
| parent | 8a002e58ec0254ef68f81175d7e41d9ed7e4dc64 (diff) | |
| parent | 944b6d88bd4286f0458aff1709ee95fa1fae0299 (diff) | |
| download | perlweeklychallenge-club-845c85cf263785b2a9c520743645f83ecef4b9a5.tar.gz perlweeklychallenge-club-845c85cf263785b2a9c520743645f83ecef4b9a5.tar.bz2 perlweeklychallenge-club-845c85cf263785b2a9c520743645f83ecef4b9a5.zip | |
Merge pull request #2136 from fluca1978/pwc75
Pwc75
| -rw-r--r-- | challenge-075/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-075/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-075/luca-ferrari/raku/ch-1.p6 | 81 | ||||
| -rw-r--r-- | challenge-075/luca-ferrari/raku/ch-2.p6 | 58 |
4 files changed, 141 insertions, 0 deletions
diff --git a/challenge-075/luca-ferrari/blog-1.txt b/challenge-075/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..37064d7a85 --- /dev/null +++ b/challenge-075/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/08/24/PerlWeeklyChallenge75.html#task1 diff --git a/challenge-075/luca-ferrari/blog-2.txt b/challenge-075/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..d96d2273a8 --- /dev/null +++ b/challenge-075/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/08/24/PerlWeeklyChallenge75.html#task2 diff --git a/challenge-075/luca-ferrari/raku/ch-1.p6 b/challenge-075/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..b52a91d40c --- /dev/null +++ b/challenge-075/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,81 @@ +#!raku + +sub find-solutions( Int :$sum, :@coins ) { + my @solutions; + + # add all the 'one coin' solutions + @solutions.push: [ $_ xx ( $sum / $_ ) ] for @coins.grep( $sum %% * ); + + + # now inspect all the other cases + for @coins.grep( $sum !%% * ).sort( { $^b <=> $^a } ) -> $current-coin { + next if $current-coin > $sum; + my @current-solution; + @current-solution.push: $current-coin; + + for @coins.grep( * !~~ $current-coin ).sort( { $^b <=> $^a } ) { + my $current-sum = [+] @current-solution; + # try to add the same number over and over again + while ( ( $current-sum + $_ ) <= $sum ) { + @current-solution.push: $_ ; + $current-sum = [+] @current-solution; + } + + if ( $current-sum == $sum ) { + # use a new array to clone it, so I can delete the last value and continue over + @solutions.push: Array.new: @current-solution.sort; + @current-solution[ *-1 ]:delete; + next; + } + } + + } + + + # last step: decompose every number in a solution into other numbers + my @decomposed-solutions; + for @solutions -> @current-solution { + for 0 ..^ @current-solution.elems -> $switching-index { + + my $current-coin = @current-solution[ $switching-index ]; + next if $current-coin ~~ 1; + + for @coins.grep( $current-coin %% * ) { + next if $_ ~~ $current-coin; + my @new-solution = Array.new: @current-solution; + @new-solution[ $switching-index ]:delete; + @new-solution[ $switching-index ] = | ( $_ xx ( $current-coin / $_ ) ); + @decomposed-solutions.push: [ @new-solution.sort ]; + } + } + } + + + # now build something unique + my %unique-solutions; + for @decomposed-solutions { + %unique-solutions{ "{ $_ }" } = $_; + } + + for @solutions { + %unique-solutions{ "{ $_ }" } = $_; + } + + + %unique-solutions.values; +} + + + + +sub MAIN( Int $S where { $S > 0 }, + *@C where { @C.grep( * ~~ Int ).elems == @C.elems } ) { + say "Coins are { @C } that must give sum $S"; + + my @solutions; + + @solutions = find-solutions( coins => @C, sum => $S ); + + @solutions.join( "\n" ).say; + +} diff --git a/challenge-075/luca-ferrari/raku/ch-2.p6 b/challenge-075/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..14cf0bef95 --- /dev/null +++ b/challenge-075/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,58 @@ +#!raku + +class Histogram { + has Int $.column; + has Int $.height; + + method Str() { "Histogram $!height Column $!column"; } +} + + +class Rectangle { + has Int $.height; + has Int $.base; + + method area() { $!height * $!base } + method Str() { "Rectangle with base $!base and height $!height ($!base x $!height)" } +} + + +sub MAIN( *@A + where { @A.grep( * ~~ Int ).elems == @A.elems && @A.grep( *.Int >= 1 ) } ) { + say "Numbers are { @A }"; + + my Histogram @histograms; + my $column = 1; + for @A { + @histograms.push: Histogram.new: column => $column, height => $_.Int; + } + + + my Rectangle @rectangles; + my ( $current-height, $current-width ) = Nil, Nil; + + for 0 ..^ @histograms.elems -> $current-index { + ( $current-height, $current-width ) = @histograms[ $current-index ].height, 0; + + + for 0 ..^ @histograms.elems { + next if $_ < $current-index && @histograms[ $_ ].height < $current-height; + if @histograms[ $_ ].height < $current-height { + last; + } + else { + $current-width++; + } + } + + @rectangles.push: Rectangle.new( height => $current-height, + base => $current-width ); + } + + + + # get the first one with the biggest area + @rectangles.sort( { $^b.area <=> $^a.area } ).first.put; + + +} |
