diff options
| -rwxr-xr-x | challenge-192/2colours/raku/ch-1.raku | 10 | ||||
| -rwxr-xr-x | challenge-192/2colours/raku/ch-2.raku | 28 |
2 files changed, 38 insertions, 0 deletions
diff --git a/challenge-192/2colours/raku/ch-1.raku b/challenge-192/2colours/raku/ch-1.raku new file mode 100755 index 0000000000..2208626234 --- /dev/null +++ b/challenge-192/2colours/raku/ch-1.raku @@ -0,0 +1,10 @@ +#!/usr/bin/env raku + +subset PosInt of Int where * > 0; + +sub MAIN(PosInt $n) { + $n.msb // 0 andthen + 2 +< $_ - 1 andthen + $_ - $n andthen + .say; +}
\ No newline at end of file diff --git a/challenge-192/2colours/raku/ch-2.raku b/challenge-192/2colours/raku/ch-2.raku new file mode 100755 index 0000000000..3a66a53045 --- /dev/null +++ b/challenge-192/2colours/raku/ch-2.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku + +subset Choice of List where ((Int, Int), Int); + +sub next-placement(@normal-values) { + @normal-values.pairs.grep(*.value != 0).rotor(2 => -1).grep({ [!=] $_>>.value>>.sign }) andthen .so ?? .min({ [R-] $_>>.key }) !! Nil +} + +multi dist-steps(@ where { .sum !%% .elems } ) { -1 } +multi dist-steps(@list) { + my $average = @list.sum div +@list; + my @norm-list = @list >>->> $average; + my @costs = gather while @norm-list.&next-placement -> $endpoints { + my $moved-amount = $endpoints.>>.value>>.abs.min; + my $distance = [R-] $endpoints>>.key; + take $moved-amount * $distance; + $_ -= $moved-amount * .sign for $endpoints>>.value; + }; + @costs.sum +} + +my token natural-number { 0 | <[1..9]> <[0..9]>* } +subset NatList of Str where /^ '(' <natural-number>* % [\s* ',' \s*] ')' $/; +sub MAIN($n) { + die 'Please provide a valid list of natural numbers' unless $n ~~ NatList; + my @list = $<natural-number>>>.Int; + @list.&dist-steps.say; +}
\ No newline at end of file |
