diff options
| -rw-r--r-- | challenge-218/arne-sommer/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-218/arne-sommer/raku/ch-1.raku | 5 | ||||
| -rwxr-xr-x | challenge-218/arne-sommer/raku/ch-2.raku | 67 | ||||
| -rwxr-xr-x | challenge-218/arne-sommer/raku/matrix-score | 67 | ||||
| -rwxr-xr-x | challenge-218/arne-sommer/raku/maximum-product | 27 | ||||
| -rwxr-xr-x | challenge-218/arne-sommer/raku/maximum-product-map | 5 | ||||
| -rwxr-xr-x | challenge-218/arne-sommer/raku/maximum-product-reduce | 5 |
7 files changed, 177 insertions, 0 deletions
diff --git a/challenge-218/arne-sommer/blog.txt b/challenge-218/arne-sommer/blog.txt new file mode 100644 index 0000000000..a4d51990ad --- /dev/null +++ b/challenge-218/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/scored-product.html diff --git a/challenge-218/arne-sommer/raku/ch-1.raku b/challenge-218/arne-sommer/raku/ch-1.raku new file mode 100755 index 0000000000..55b4b66a98 --- /dev/null +++ b/challenge-218/arne-sommer/raku/ch-1.raku @@ -0,0 +1,5 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@list where @list.elems >= 3 && all(@list) ~~ Int); + +say @list.combinations(3)>>.reduce(&infix:<*>).max; diff --git a/challenge-218/arne-sommer/raku/ch-2.raku b/challenge-218/arne-sommer/raku/ch-2.raku new file mode 100755 index 0000000000..e1ddeacb8a --- /dev/null +++ b/challenge-218/arne-sommer/raku/ch-2.raku @@ -0,0 +1,67 @@ +#! /usr/bin/env raku + +unit sub MAIN (Str $matrix = "0 0 0 1 | 1 0 1 0 | 1 1 0 0", :v(:$verbose)); + +my @matrix = $matrix.split("|")>>.words>>.Array; +my @rows = @matrix>>.elems; + +die "Must have at least 1 row" unless @rows.elems >= 1; +die "The rows must have the same size" unless [==] @rows; +die "Must contain 0 and 1 only" unless all(@matrix[*;*]) eq any(0,1); + +my $cols = @matrix[0].elems; + +say ": Matrix: { @matrix.raku } " if $verbose; +say ": - Size: rows:$rows cols:$cols" if $verbose; + +for ^$rows -> $row-id +{ + print ": - row $row-id " if $verbose; + + if @matrix[$row-id][0] == 0 + { + say " - swapping" if $verbose; + @matrix.&flip-row($row-id); + say ": Matrix: { @matrix.raku } " if $verbose; + } + else + { + say " - ok" if $verbose; + } +} + +for ^$cols -> $col-id +{ + my @cols = (^@matrix.elems).map({ @matrix[$_][$col-id] }); + + print ": - col $col-id -> @cols[]" if $verbose; + + if @cols.sum < $cols / 2 + { + say " - swapping" if $verbose; + @matrix.&flip-col($col-id); + say ": Matrix: { @matrix.raku } " if $verbose; + } + else + { + say " - ok" if $verbose; + } +} + +say ": Final matrix: { @matrix.raku }" if $verbose; + +say @matrix.map({ @$_.join.parse-base(2) }).sum; + +sub flip-row(@matrix, UInt $row) +{ + @(@matrix[$row]).=map({ $_ == 1 ?? 0 !! 1 } ); + + return @matrix; +} + +sub flip-col(@matrix, UInt $col) +{ + (^@matrix.elems).map({ @matrix[$_][$col] = + ! + @matrix[$_][$col] }); + + return @matrix; +}
\ No newline at end of file diff --git a/challenge-218/arne-sommer/raku/matrix-score b/challenge-218/arne-sommer/raku/matrix-score new file mode 100755 index 0000000000..e1ddeacb8a --- /dev/null +++ b/challenge-218/arne-sommer/raku/matrix-score @@ -0,0 +1,67 @@ +#! /usr/bin/env raku + +unit sub MAIN (Str $matrix = "0 0 0 1 | 1 0 1 0 | 1 1 0 0", :v(:$verbose)); + +my @matrix = $matrix.split("|")>>.words>>.Array; +my @rows = @matrix>>.elems; + +die "Must have at least 1 row" unless @rows.elems >= 1; +die "The rows must have the same size" unless [==] @rows; +die "Must contain 0 and 1 only" unless all(@matrix[*;*]) eq any(0,1); + +my $cols = @matrix[0].elems; + +say ": Matrix: { @matrix.raku } " if $verbose; +say ": - Size: rows:$rows cols:$cols" if $verbose; + +for ^$rows -> $row-id +{ + print ": - row $row-id " if $verbose; + + if @matrix[$row-id][0] == 0 + { + say " - swapping" if $verbose; + @matrix.&flip-row($row-id); + say ": Matrix: { @matrix.raku } " if $verbose; + } + else + { + say " - ok" if $verbose; + } +} + +for ^$cols -> $col-id +{ + my @cols = (^@matrix.elems).map({ @matrix[$_][$col-id] }); + + print ": - col $col-id -> @cols[]" if $verbose; + + if @cols.sum < $cols / 2 + { + say " - swapping" if $verbose; + @matrix.&flip-col($col-id); + say ": Matrix: { @matrix.raku } " if $verbose; + } + else + { + say " - ok" if $verbose; + } +} + +say ": Final matrix: { @matrix.raku }" if $verbose; + +say @matrix.map({ @$_.join.parse-base(2) }).sum; + +sub flip-row(@matrix, UInt $row) +{ + @(@matrix[$row]).=map({ $_ == 1 ?? 0 !! 1 } ); + + return @matrix; +} + +sub flip-col(@matrix, UInt $col) +{ + (^@matrix.elems).map({ @matrix[$_][$col] = + ! + @matrix[$_][$col] }); + + return @matrix; +}
\ No newline at end of file diff --git a/challenge-218/arne-sommer/raku/maximum-product b/challenge-218/arne-sommer/raku/maximum-product new file mode 100755 index 0000000000..a8af339369 --- /dev/null +++ b/challenge-218/arne-sommer/raku/maximum-product @@ -0,0 +1,27 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@list where @list.elems >= 3 && all(@list) ~~ Int, + :v(:$verbose)); + +my $highest = - Inf; +my @highest; + +for @list.combinations(3) -> @candidate +{ + my $product = [*] @candidate; + + print ":Candidate { @candidate.join(",") } with product: $product" if $verbose; + + if $product > $highest + { + $highest = $product; + @highest = @candidate; + print " - highest" if $verbose; + } + + say ""; +} + +say ":Highest triple { @highest.join(",") }" if $verbose; + +say $highest; diff --git a/challenge-218/arne-sommer/raku/maximum-product-map b/challenge-218/arne-sommer/raku/maximum-product-map new file mode 100755 index 0000000000..863fbec451 --- /dev/null +++ b/challenge-218/arne-sommer/raku/maximum-product-map @@ -0,0 +1,5 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@list where @list.elems >= 3 && all(@list) ~~ Int); + +say @list.combinations(3).map({ [*] @_ }).max; diff --git a/challenge-218/arne-sommer/raku/maximum-product-reduce b/challenge-218/arne-sommer/raku/maximum-product-reduce new file mode 100755 index 0000000000..55b4b66a98 --- /dev/null +++ b/challenge-218/arne-sommer/raku/maximum-product-reduce @@ -0,0 +1,5 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@list where @list.elems >= 3 && all(@list) ~~ Int); + +say @list.combinations(3)>>.reduce(&infix:<*>).max; |
