From 297d90bbf46708d255d3cb09b84a992f84150dbe Mon Sep 17 00:00:00 2001 From: Scott Sotka Date: Fri, 12 Jan 2024 13:27:57 -0800 Subject: CH-251 part 2, lucky numbers --- challenge-251/bn-ssotka/raku/ch-2.raku | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 challenge-251/bn-ssotka/raku/ch-2.raku diff --git a/challenge-251/bn-ssotka/raku/ch-2.raku b/challenge-251/bn-ssotka/raku/ch-2.raku new file mode 100755 index 0000000000..7dc69e08ce --- /dev/null +++ b/challenge-251/bn-ssotka/raku/ch-2.raku @@ -0,0 +1,38 @@ +#!/usr/bin/env raku + +# You are given a m x n matrix of distinct numbers. + +# Write a script to return the lucky number, if there is one, or -1 if not. + +# A lucky number is an element of the matrix such that it is +# the minimum element in its row and maximum in its column. + +sub get-lucky (@matrix) { + my @row_mins = @matrix.map: *.min; # get the mins of each row + my @rotated_matrix = [Z] @matrix; # rotate the matrix + my @col_maxs = @rotated_matrix.map: *.max; # get the max of each of the rotated rows + + my @lucky = @row_mins ∩ @col_maxs; # get the intersection of the two + return @lucky ?? @lucky[0] !! -1; +} +my @test-matricies = ( + ( + [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17] + ), + ( + [ 1, 10, 4, 2], + [ 9, 3, 8, 7], + [15, 16, 17, 12] + ), + ( + [7 ,8], + [1 ,2] + ) +); + +for @test-matricies -> @matrix { + say @matrix; + say get-lucky(@matrix); +} -- cgit