From 3b7b3ffadd94eb8372845044cde2fcf34b30afa5 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 24 Aug 2020 14:15:46 +0200 Subject: Task 1 done. --- challenge-075/luca-ferrari/raku/ch-1.p6 | 81 +++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 challenge-075/luca-ferrari/raku/ch-1.p6 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; + +} -- cgit From 4bcb5f6529029f0a608b775c2d2fddbaa90a5dbf Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 24 Aug 2020 15:02:41 +0200 Subject: Task 2 done. --- challenge-075/luca-ferrari/raku/ch-2.p6 | 58 +++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 challenge-075/luca-ferrari/raku/ch-2.p6 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; + + +} -- cgit From 944b6d88bd4286f0458aff1709ee95fa1fae0299 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 24 Aug 2020 15:28:48 +0200 Subject: Blog references --- challenge-075/luca-ferrari/blog-1.txt | 1 + challenge-075/luca-ferrari/blog-2.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 challenge-075/luca-ferrari/blog-1.txt create mode 100644 challenge-075/luca-ferrari/blog-2.txt 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 -- cgit