aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrir <rirans@comcast.net>2023-05-28 18:08:18 -0400
committerrir <rirans@comcast.net>2023-05-28 18:08:18 -0400
commitf6a6cea4b1f736b511072170beae118bb1ee243f (patch)
treef8d9c9e86253b0de15cfc13f32bc9cfe4cc2ff8f
parent1c9d28484fe0cca463f5b0a55fdf9e0015dc868c (diff)
downloadperlweeklychallenge-club-f6a6cea4b1f736b511072170beae118bb1ee243f.tar.gz
perlweeklychallenge-club-f6a6cea4b1f736b511072170beae118bb1ee243f.tar.bz2
perlweeklychallenge-club-f6a6cea4b1f736b511072170beae118bb1ee243f.zip
218
-rw-r--r--challenge-218/0rir/raku/ch-1.raku33
-rw-r--r--challenge-218/0rir/raku/ch-2.raku46
2 files changed, 23 insertions, 56 deletions
diff --git a/challenge-218/0rir/raku/ch-1.raku b/challenge-218/0rir/raku/ch-1.raku
index 330c2aa768..287fd175f5 100644
--- a/challenge-218/0rir/raku/ch-1.raku
+++ b/challenge-218/0rir/raku/ch-1.raku
@@ -29,7 +29,6 @@ Input: @list = (-8, 2, -9, 0, -4, 3)
Output: 216
-9 × -8 × 3 => 216
-
=end comment
my @Test =
@@ -39,9 +38,7 @@ my @Test =
(-8, 2, -9, 0, -4, 3), 216,
(-1, 0, 1, 3, 1), 3,
-# (-3, -2, -1, 0), 0,
-
- ( 2, 3, 5), 30, # three elems
+ (-2, 3, 5), -30, # three elems
( 0, 1, 2, 3), 6, # quads
( 1, 2, 3, 4), 24,
@@ -77,46 +74,32 @@ my @Test =
(-8, -4, -2, 5, 7, 9), 315,
(-8, -5, -2, 5, 7, 9), 360,
;
-
-plan @Test ÷ 2 + 1 + 2_005_000;
-
+plan @Test ÷ 2 + 1 + 1000;
multi max3prod( @a where *.elems < 3) { die( 'oospec') }
-#multi max3prod( @a where *.elems == 3) { [×] @a } # triad
-#multi max3prod( @a where *.elems == 4) { # quad
-# @a.combinations(3).map( {[*] $_} ).max }
multi max3prod( @a ) {
multi func {…}
return func @a.sort;
- # more than 4 elems
-# multi func( @a where *[*-1] == 0) { 0 } # max0
multi func( @a where 0 ≤ *[0]) { [×] @a[*-3..*-1] } # all pos
multi func( @a where *[*-1] < 0) { [×] @a[*-3..*-1] } # all neg
# mixed signs
-# multi func( @a where $_[0] < 0 < $_[1] ) { [×] @a[*-3..*-1] } # 1 neg
-#multi func( @a where $_[*-2] < 0 < $_[*-1]) { [×] @a[0,1,*-1] } # 1 pos
-# multi func( @a where $_[*-3] < 0 < $_[*-2]) { # 2 pos
-# max ([×] @a[0,1,*-1]), ([×] @a[*-3..*-1]); }
-# multi func( @a where $_[1] < 0 < $_[2] ) { # 2 neg
-# max ([×] @a[0,1,*-1]), ([×] @a[*-3..*-1]); }
- multi func( @a ) { # default
- max ([×] @a[0,1,*-1]), ([×] @a[*-3..*-1]); }
+ multi func( @a){ max ([×] @a[0,1,*-1]), ([×] @a[*-3..*-1]); }
}
for @Test -> @in, $exp {
- is max3prod(@in), $exp, "$exp\t<- @in.sort()";
+ is max3prod(@in), $exp, "$exp\t<- @in.sort() sorted";
}
-srand(104571);
-hyper for 1..2_005_000 {
+srand(102);
+for 1..1_000 {
my @t;
for 1..4 + (^5).pick -> $l {
for (^20).pick - 10 -> $r {
@t.push: $r;
}
}
- my $res = @t.combinations(3).map( {[*] $_} ).max;
- is max3prod(@t), $res, "$res\t<- @t.sort()";
+ my $res = @t.combinations(3).map( {[*] $_} ).max; # brute
+ is max3prod(@t), $res, "$res\t<- @t.sort() sorted";
}
dies-ok { max3prod( (1,2)) }, "(1,2) dies-ok";
diff --git a/challenge-218/0rir/raku/ch-2.raku b/challenge-218/0rir/raku/ch-2.raku
index c3ea07ad02..2decd60d85 100644
--- a/challenge-218/0rir/raku/ch-2.raku
+++ b/challenge-218/0rir/raku/ch-2.raku
@@ -1,7 +1,6 @@
#!/usr/bin/env raku
# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
use v6.d;
-use lib $?FILE.IO.parent(2).add("lib");
use Test;
=begin comment
@@ -9,15 +8,13 @@ Task 2: Matrix Score Submitted by: Mohammad S Anwar
Given a m x n binary matrix i.e. having only 1 and 0, make as many
moves as you want to get the highest score.
-A move can be either toggling each value in a row or column.
+A move can be either toggling all values in a row or column.
-To get the score, convert the each row's binary value to decimal and
+To get the score, convert each row's binary value to decimal and
return the sum.
Example 1:
-Input: @matrix = [ [0,0,1,1],
- [1,0,1,0],
- [1,1,0,0], ]
+Input: @matrix = [ [0,0,1,1], [1,0,1,0], [1,1,0,0], ]
Output: 39
Move #1: convert row #1 => 1100
@@ -58,26 +55,28 @@ sub max-row-base2( @m -->Int) {
if +@col.grep(1) < +@col.grep(0) { invert @col; }
}
return [+] @mat.map( { parse-base .join, 2});
+
+ sub invert( @a -->Nil) { for @a -> $e is rw { $e = (!$e.Bool).Int }}
}
-sub invert( @a -->Nil) { for @a -> $e is rw { $e = (!$e.Bool).Int }}
-sub is-row( @m, UInt $i -->Bool) { $i < @m.elems }
sub bind-col( @m, $col-num -->Array) {
my @col;
@col[$_] := @m[$_][$col-num] for ^@m;
@col
}
-# A very stupid brute.
-# Identify @matrix rows by their natural indices, and columns starting
-# after @matrix.end. Permute this sequence and use the results to "toggle"
-# the matrix in all permutations. Score after every toggle.
+# A very stupid, stupid brute. Was I redundent?
+#
+# Every row and column is named. Rows use their index and columns are
+# named with the @matrix.end + the value of the index into @matrix[0].
+# Permute this list; go thru each list; toggle each column or row; update
+# score and repeat. Quit early if theoretical best score is found.
sub brute( @matrix -->Int) {
my $ct = 0;
my @mat = @matrix.raku.EVAL;
my $theo-best = theo-best(@mat);
my $best = score( @mat);
- my @plan = ( ^(@mat.end +@mat[0] + 1)).Array.permutations;
+ my @plan = ( ^(@mat.elems +@mat[0])).Array.permutations;
for @plan -> @p {
my @m = @mat;
for @p -> $id {
@@ -104,27 +103,12 @@ sub toggle( UInt $id, @a -->Nil) {
@a[$r][$cid] = (!@a[$r][$cid].Bool).Int;
}
}
+ sub is-row( @m, UInt $i -->Bool) { $i < @m.elems }
}
-sub idsay( @m, $id -->Bool) { # Display a row or column.
- if is-row @m, $id {
- say @m[$id].raku();
- } else {
- my $cid = $id - @m.elems;
- my @col;
- for 0..^@m -> $i { print " @m[$i][$cid]"; }
- }
-}
-sub msay( @m --> Nil) { # Display a matrix.
- print "[";
- for 0..^(@m - 1) -> $i { say "\t@m[$i].raku()"; }
- say "\t@m[*-1].raku() ]";
-}
-
-
for @Test -> @in, $exp {
- is max-row-base2( @in), $exp, "$exp <-\t@in.raku()";
-# is brute( @in), $exp, "$exp <-\t@in.raku()";
+ is max-row-base2( @in), $exp, "$exp <-\t@in.raku()";
+# is brute( @in), $exp, "$exp <-\t@in.raku()";
}
done-testing;