aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2022-11-22 08:31:46 +0100
committerLuca Ferrari <fluca1978@gmail.com>2022-11-22 08:31:46 +0100
commita74050e508332d5b3dfafc00835151b9054729f0 (patch)
treea0b5b0ec5c4ae7eb4ae26db48afe3f2c6a7ca379
parent0b873ce7997b6f0b42f1db9e6eaf9dceacceb4d2 (diff)
downloadperlweeklychallenge-club-a74050e508332d5b3dfafc00835151b9054729f0.tar.gz
perlweeklychallenge-club-a74050e508332d5b3dfafc00835151b9054729f0.tar.bz2
perlweeklychallenge-club-a74050e508332d5b3dfafc00835151b9054729f0.zip
Task 2 done
-rw-r--r--challenge-192/luca-ferrari/raku/ch-2.p634
1 files changed, 34 insertions, 0 deletions
diff --git a/challenge-192/luca-ferrari/raku/ch-2.p6 b/challenge-192/luca-ferrari/raku/ch-2.p6
new file mode 100644
index 0000000000..44ddbb5c40
--- /dev/null
+++ b/challenge-192/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,34 @@
+#!raku
+
+# Perl Weekly Challenge 192
+
+sub MAIN( *@n is copy where { @n.elems == @n.grep( * ~~ Int ).elems }
+ , :$verbose = False ) {
+ my $elem = @n.sum / @n.elems;
+ '-1'.say and exit if ( $elem.Int !~~ $elem );
+
+ my @moves;
+ @moves.push: [@n];
+ while ( @n.grep( * ~~ $elem ).elems != @n.elems ) {
+ for 0 ..^ @n.elems -> $index {
+ if ( @n[ $index ] == @n.max ) {
+ for 0 ..^ @n.elems -> $borrow {
+ next if $borrow == $index;
+ next if @n[ $borrow ] >= @n[ $index ];
+ next if @n[ $borrow ] >= $elem;
+ @n[ $borrow ]++;
+ last;
+ }
+
+ @n[ $index ]--;
+ }
+
+ }
+
+ @moves.push: [@n];
+ }
+
+
+ @moves.join( "\n" ).say if $verbose;
+ @moves.elems.say;
+}