aboutsummaryrefslogtreecommitdiff
path: root/challenge-083/gugod
diff options
context:
space:
mode:
authorKang-min Liu <gugod@gugod.org>2020-10-20 20:29:53 +0900
committerKang-min Liu <gugod@gugod.org>2020-10-20 20:29:53 +0900
commitfeb736f09447fe7745e07d1bf71beddcf4a30275 (patch)
treea3395c518f758e8add30d4d9b0174c3a80b97563 /challenge-083/gugod
parent000e17bf6effec267a6e848f77889acb06a65abe (diff)
downloadperlweeklychallenge-club-feb736f09447fe7745e07d1bf71beddcf4a30275.tar.gz
perlweeklychallenge-club-feb736f09447fe7745e07d1bf71beddcf4a30275.tar.bz2
perlweeklychallenge-club-feb736f09447fe7745e07d1bf71beddcf4a30275.zip
exit early
Diffstat (limited to 'challenge-083/gugod')
-rw-r--r--challenge-083/gugod/raku/ch-2.raku10
1 files changed, 5 insertions, 5 deletions
diff --git a/challenge-083/gugod/raku/ch-2.raku b/challenge-083/gugod/raku/ch-2.raku
index 540dc475a5..55020e4cfc 100644
--- a/challenge-083/gugod/raku/ch-2.raku
+++ b/challenge-083/gugod/raku/ch-2.raku
@@ -4,20 +4,20 @@ sub flip-elems (@n) {
my $sum = [+] @n;
my @min_combination = @n;
my $min_sum = Inf;
- my $min_k = 0;
-
- for 1..@n.elems - 1 -> $k {
+ K: for 1..@n.elems - 1 -> $k {
for @n.combinations($k) -> @c {
my $s = $sum - 2 * ([+] @c);
if 0 <= $s < $min_sum {
@min_combination = @c;
$min_sum = $s;
- $min_k = $k;
+ if $s == 0 {
+ last K;
+ }
}
}
}
say ">> $min_sum = ([+] {@n}) - 2 * ([+] {@min_combination})";
- return $min_k;
+ return @min_combination.elems;
}
# say flip-elems [2,13,10,8]; # 2