diff options
| -rw-r--r-- | challenge-251/simon-proctor/raku/ch-1.raku | 28 | ||||
| -rw-r--r-- | challenge-251/simon-proctor/raku/ch-2.raku | 42 |
2 files changed, 70 insertions, 0 deletions
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..17ae44be46 --- /dev/null +++ b/challenge-251/simon-proctor/raku/ch-2.raku @@ -0,0 +1,42 @@ +#!/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 #= Matrix values + && all(@vals) ~~ IntStr + && @vals.elems %% $width, +) { + 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]; +} |
