From 275425a6539c092ed817bba98f9bf3bf910f4897 Mon Sep 17 00:00:00 2001 From: Scimon Date: Mon, 8 Jan 2024 10:10:08 +0000 Subject: Challenge 251 --- challenge-251/simon-proctor/raku/ch-1.raku | 28 +++++++++++++++++++++ challenge-251/simon-proctor/raku/ch-2.raku | 40 ++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 challenge-251/simon-proctor/raku/ch-1.raku create mode 100644 challenge-251/simon-proctor/raku/ch-2.raku diff --git a/challenge-251/simon-proctor/raku/ch-1.raku b/challenge-251/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..a85d9e7171 --- /dev/null +++ b/challenge-251/simon-proctor/raku/ch-1.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku + +#| Run Test Suite +multi sub MAIN('test') { + use Test; + is concat-number(6, 12, 25, 1), 1286; + is concat-number(10, 7, 31, 5, 2, 2), 489; + is concat-number(1, 2, 10), 112; + done-testing; +} + +#| Print the concatentaion number +multi sub MAIN( + *@vals where all(@vals) ~~ IntStr #= List of ints +) { + concat-number( |@vals ).say; +} + +sub concat-number( *@vals ) { + my $total = 0; + while (@vals.elems > 1) { + my $f = @vals.shift; + my $l = @vals.pop; + $total += ($f ~ $l); + } + $total += @vals[0] if @vals.elems == 1; + return $total; +} diff --git a/challenge-251/simon-proctor/raku/ch-2.raku b/challenge-251/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..e5f95f5216 --- /dev/null +++ b/challenge-251/simon-proctor/raku/ch-2.raku @@ -0,0 +1,40 @@ +#!/usr/bin/env raku + +#| Run the Test Suite +multi sub MAIN('test') { + use Test; + is-deeply rot-matrix([[1,2],[3,4]]), [[1,3],[2,4]]; + is lucky( [[3,7,8],[9,11,13],[15,16,17]] ), 15; + is lucky( [[1,10,4,2],[9,3,8,7],[15,16,17,12]] ), 12; + is lucky( [[7,8],[1,2]] ), 7; + is lucky( [[1,2],[3,4]] ), 3; + done-testing; +} + +multi sub MAIN( + Int $width, #= Matrix width + *@vals where @vals.unique.elems == @vals.elems && all(@vals) ~~ IntStr, #= Matrix values +) { + my @matrix = @vals.rotor($width); + lucky(@matrix).say; +} + +sub rot-matrix( @matrix ) { + my @out; + for (^@matrix.elems) -> $i { + for (^@matrix[0].elems) -> $j { + @out[$j] //= []; + @out[$j][$i] = @matrix[$i][$j]; + } + } + return @out; +} + +sub lucky( @matrix ) { + my @rot = rot-matrix(@matrix); + my @mins = @matrix.map( *.min ); + my @maxs = @rot.map( *.max ); + my $int = @mins ∩ @maxs; + return -1 unless $int.keys.elems == 1; + return $int.keys[0]; +} -- cgit From 9ba7d42d06bfcfa6762c442611c9d84a01a422ca Mon Sep 17 00:00:00 2001 From: Scimon Date: Mon, 8 Jan 2024 10:11:42 +0000 Subject: Challenge 251 (Small updateto part 2) --- challenge-251/simon-proctor/raku/ch-2.raku | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/challenge-251/simon-proctor/raku/ch-2.raku b/challenge-251/simon-proctor/raku/ch-2.raku index e5f95f5216..17ae44be46 100644 --- a/challenge-251/simon-proctor/raku/ch-2.raku +++ b/challenge-251/simon-proctor/raku/ch-2.raku @@ -13,7 +13,9 @@ multi sub MAIN('test') { multi sub MAIN( Int $width, #= Matrix width - *@vals where @vals.unique.elems == @vals.elems && all(@vals) ~~ IntStr, #= Matrix values + *@vals where @vals.unique.elems == @vals.elems #= Matrix values + && all(@vals) ~~ IntStr + && @vals.elems %% $width, ) { my @matrix = @vals.rotor($width); lucky(@matrix).say; -- cgit