diff options
| author | Philip Hood <hood@panix.com> | 2021-01-31 17:26:56 -0500 |
|---|---|---|
| committer | Philip Hood <hood@panix.com> | 2021-01-31 17:26:56 -0500 |
| commit | 2cb9d8c19ff75eded76a134d1e61929e16089021 (patch) | |
| tree | 6a026b315112e24469b7261dbca7cd9a795c5024 | |
| parent | 7245a645e20d2f2cc452f712ba35a8ee3554c911 (diff) | |
| download | perlweeklychallenge-club-2cb9d8c19ff75eded76a134d1e61929e16089021.tar.gz perlweeklychallenge-club-2cb9d8c19ff75eded76a134d1e61929e16089021.tar.bz2 perlweeklychallenge-club-2cb9d8c19ff75eded76a134d1e61929e16089021.zip | |
challenge 97, 1 and 2, in raku
| -rwxr-xr-x | challenge-097/pkmnx/raku/ch-1.raku | 6 | ||||
| -rwxr-xr-x | challenge-097/pkmnx/raku/ch-2.raku | 38 |
2 files changed, 44 insertions, 0 deletions
diff --git a/challenge-097/pkmnx/raku/ch-1.raku b/challenge-097/pkmnx/raku/ch-1.raku new file mode 100755 index 0000000000..3efce54d15 --- /dev/null +++ b/challenge-097/pkmnx/raku/ch-1.raku @@ -0,0 +1,6 @@ +#!/usr/bin/env raku + +sub MAIN(Str $S, Int $N ) { + my $fn = -> $x { ((($x.ord +'A'.ord -($N)) %26) +'A'.ord).chr }; + $S.words.map({ $_.comb().map( {$fn($_)}).join("") }).join(" ").say +} diff --git a/challenge-097/pkmnx/raku/ch-2.raku b/challenge-097/pkmnx/raku/ch-2.raku new file mode 100755 index 0000000000..e26ee03f7b --- /dev/null +++ b/challenge-097/pkmnx/raku/ch-2.raku @@ -0,0 +1,38 @@ +#!/usr/bin/env raku + +sub MAIN(Str $B, Int $S ) { + + die "$B not divisible by $S!" if $B.chars % $S != 0; + + my ( $min, $minh, $cost ) = (Inf, Inf, {}); + + for ( $B.comb().rotor($S).combinations(2) ) -> ($f,$s) { + my $df = 0; + $df++ if ( $f[$_] ne $s[$_] ) for (^$f); + $cost{ $f.join("") }{ $s.join("") } = $df; + $cost{ $s.join("") }{ $f.join("") } = $df; + } + + for ($cost.kv) -> $k,$v { + my $tot = 0; + for ($v.kv) -> $kv,$vv { + $tot += $vv + } + if ( $tot < $min ) { + $min = $tot if $tot < $min; + $minh = $k; + } + } + + if ( $min != Inf ) { + printf("Input: \$B = \"%s\", \$S = \"%d\"\n", $B, $S); + printf("Output: %d\n\nBinary Substrings (aside from self: \"%s\"):\n", $min, $minh); + + for ( $cost{$minh}.kv ) -> $k,$v { + if ( $k ne $minh ) { + printf(" \"%s\": %d flip%s to make it \"%s\"\n", $k, $v, $v>1??"s"!!'', $minh); + } + } + print("\n"); + } +} |
