diff options
| author | Polgár Márton <polgar@astron.hu> | 2022-12-18 01:43:06 +0100 |
|---|---|---|
| committer | Polgár Márton <polgar@astron.hu> | 2022-12-18 01:43:06 +0100 |
| commit | 571389cf630a6fc1d05bee5b87689c2d282b4598 (patch) | |
| tree | fefc750755f18e676114b038b108cfd9fc34109a | |
| parent | 354f57ebd708492f1c50b05c6df41b894b795492 (diff) | |
| download | perlweeklychallenge-club-571389cf630a6fc1d05bee5b87689c2d282b4598.tar.gz perlweeklychallenge-club-571389cf630a6fc1d05bee5b87689c2d282b4598.tar.bz2 perlweeklychallenge-club-571389cf630a6fc1d05bee5b87689c2d282b4598.zip | |
Current weeklies by 2colours
| -rwxr-xr-x | challenge-195/2colours/raku/ch-1.raku | 29 | ||||
| -rwxr-xr-x | challenge-195/2colours/raku/ch-2.raku | 15 |
2 files changed, 44 insertions, 0 deletions
diff --git a/challenge-195/2colours/raku/ch-1.raku b/challenge-195/2colours/raku/ch-1.raku new file mode 100755 index 0000000000..12c3887e72 --- /dev/null +++ b/challenge-195/2colours/raku/ch-1.raku @@ -0,0 +1,29 @@ +#!/usr/bin/env raku + +subset PosInt of Int where * > 0; + +multi count-special-until(PosInt $max) { + samewith .chars, .comb, '0' .. '9', :first-digit given $max +} + +multi count-special-until(1, @max, Set() $not-used, Bool :$first-digit = False --> PosInt) { + $not-used.keys.grep({ not $_ after @max.head || $first-digit && $_ eq '0' }).elems +} + +multi count-special-until(PosInt $digits, @max, Set() $not-used, Bool :$first-digit = False --> Int) { + my $classified = $not-used.keys.classify(* cmp @max[0]); + my Set() $less = $classified{Less} // Empty; + $less (-)= '0' if $first-digit; + my Set() $same = $classified{Same} // Empty; + my $result = 0; + $result += samewith($digits - 1, [ ** ], $not-used, :first-digit) if $first-digit; + $result += $less * samewith($digits - 1, [ ** ], $not-used (-) $less.keys[0]) if $less > 0; # doesn't matter for the calculation which digit is marked as used + $result += samewith($digits - 1, @max.skip, $not-used (-) $same.keys[0]) if $same; + $result +} + +sub MAIN( + PosInt $n #= upper limit for counting special numbers +) { + $n.&count-special-until.say; +} diff --git a/challenge-195/2colours/raku/ch-2.raku b/challenge-195/2colours/raku/ch-2.raku new file mode 100755 index 0000000000..451a94d037 --- /dev/null +++ b/challenge-195/2colours/raku/ch-2.raku @@ -0,0 +1,15 @@ +#!/usr/bin/env raku + +my token integer { 0 | '-'? <[1..9]> <[0..9]>* } +subset IntList of Str where /^ '(' <integer>* % ',' ')' $/; # The task didn't specify what a "list of numbers" should be - treating it as a list of integers + +sub MAIN( + $list #= list of integer numbers +) { + die 'Please provide valid input for @list' unless $list.subst(/\s/, '', :g) ~~ IntList; + my Int() @list = $<integer>; + @list.grep(* %% 2).Bag andthen + .so ?? .max({ .value, - .key }).key !! -1 andthen + .say; + +} |
