From 02ef15bd7e68e35a558c767dacf1be6afb818581 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 22 May 2023 12:20:10 +0000 Subject: Challenge 218 Solutions (Raku) --- challenge-218/mark-anderson/raku/ch-1.raku | 40 +++++++++++++++++++++++++++ challenge-218/mark-anderson/raku/ch-2.raku | 43 ++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 challenge-218/mark-anderson/raku/ch-1.raku create mode 100644 challenge-218/mark-anderson/raku/ch-2.raku diff --git a/challenge-218/mark-anderson/raku/ch-1.raku b/challenge-218/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..3c1936be9b --- /dev/null +++ b/challenge-218/mark-anderson/raku/ch-1.raku @@ -0,0 +1,40 @@ +#!/usr/bin/env raku +use Test; + +is max-product(3,1,2), 6; +is max-product(4,1,3,2), 24; +is max-product(-1,0,1,3,1), 3; +is max-product(1,-2,3), -6; +is max-product(4,0,-3,5), 0; +is max-product(-3,-2,0,-4), 0; +is max-product(-3,-2,1,-4), 12; +is max-product(-3,-5,-2,-4), -24; +is max-product(-1,-1,1,1), 1; +is max-product(-1,-1,-1,0), 0; +is max-product(-1,-1,-1,1), 1; +is max-product(1,1,1,0), 1; +is max-product(0,0,0,0), 0; +is max-product(1,2,0,-1), 0; +is max-product(-1,0,0,-1), 0; + +multi max-product(+$a where .elems == 3) +{ + [*] |$a +} + +multi max-product(+$a) +{ + my %c = $a.classify({ .sign }); + + return [*] $a.sort.tail(3) if $a == any %c.values; + + my @neg = %c{-1}.sort.head(2); + my @pos = %c{1}.sort.tail(3); + + max gather + { + take 0; + take [*] @pos if @pos == 3; + take [*] |@neg, @pos.max if @neg == 2 + } +} diff --git a/challenge-218/mark-anderson/raku/ch-2.raku b/challenge-218/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..3a3248057e --- /dev/null +++ b/challenge-218/mark-anderson/raku/ch-2.raku @@ -0,0 +1,43 @@ +#!/usr/bin/env raku +use Test; + +is matrix-score([0,0,1,1], + [1,0,1,0], + [1,1,0,0]), 39; + +is matrix-score([0]), 1; + +is matrix-score([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], + [0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0], + [0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0], + [0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0], + [0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0], + [0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0], + [0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0], + [0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0], + [0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0], + [0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0], + [0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0], + [0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0], + [0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0], + [0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0], + [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]), 2246857; + +sub matrix-score(+@matrix) +{ + $_ = .map(* +^ 1).Array unless .[0] for @matrix; + + @matrix = ([Z] @matrix)>>.Array.Array; + + for @matrix.skip + { + my %b = .Bag; + $_ = .map(* +^ 1).Array if (%b<0> // 0) > (%b<1> // 0) + } + + [+] ([Z] @matrix).map({ .join.parse-base(2) }) +} -- cgit