diff options
| author | Andrew Shitov <andy@shitov.ru> | 2020-10-19 08:21:00 +0200 |
|---|---|---|
| committer | Andrew Shitov <andy@shitov.ru> | 2020-10-19 08:21:00 +0200 |
| commit | 1fbc7a35445517eb6c67622eec9213420c5b2ed7 (patch) | |
| tree | 24269b3c78f582fa0b810db8b8cc9afd448a234b /challenge-083/ash | |
| parent | d46f83e37e3c252e2f566da9a78f7ad8c8254671 (diff) | |
| download | perlweeklychallenge-club-1fbc7a35445517eb6c67622eec9213420c5b2ed7.tar.gz perlweeklychallenge-club-1fbc7a35445517eb6c67622eec9213420c5b2ed7.tar.bz2 perlweeklychallenge-club-1fbc7a35445517eb6c67622eec9213420c5b2ed7.zip | |
Solutions 083-1 and 083-2 by ash
Diffstat (limited to 'challenge-083/ash')
| -rw-r--r-- | challenge-083/ash/raku/ch-1.raku | 13 | ||||
| -rw-r--r-- | challenge-083/ash/raku/ch-2.raku | 34 |
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-083/ash/raku/ch-1.raku b/challenge-083/ash/raku/ch-1.raku new file mode 100644 index 0000000000..46ec086f54 --- /dev/null +++ b/challenge-083/ash/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku +# +# Task 1 from +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-083/ + +# Output: +# $ raku ch-1.raku +# 23 + +my $s = 'The purpose of our lives is to be happy'; +# my $s = 'The Weekly Challenge'; + +$s.words[1 .. *-2]>>.chars.sum.say; diff --git a/challenge-083/ash/raku/ch-2.raku b/challenge-083/ash/raku/ch-2.raku new file mode 100644 index 0000000000..0b38dd9979 --- /dev/null +++ b/challenge-083/ash/raku/ch-2.raku @@ -0,0 +1,34 @@ +#!/usr/bin/env raku +# +# Task 2 from +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-083/ + +my @a = 12, 2, 100; +# Number of sign flips: 2; min sum: 86 + +# my @a = 12, 2, 10; +# Number of sign flips: 1; min sum: 0 + +# my @a = 3, 10, 8; +# Number of sign flips: 1; min sum: 1 + +my $min_sum = Inf; +my $min_flips = Inf; +my $min_comb = ''; + +my $fmt = '%0' ~ @a.elems ~ 'b'; + +for ^ 2**@a -> $n { + my @bits = $n.fmt($fmt).comb; + + my $sum = [+] @bits>>.subst(0, -1) Z* @a; + next if $sum < 0; + + if $sum < $min_sum { + $min_sum = $sum; + my $flips = @bits.grep(0).elems; + $min_flips = $flips if $flips < $min_flips; + } +} + +say "Number of sign flips: $min_flips; min sum: $min_sum"; |
