aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuis Mochan <mochan@fis.unam.mx>2023-12-03 16:42:33 -0600
committerLuis Mochan <mochan@fis.unam.mx>2023-12-03 16:42:33 -0600
commit5d7e82ad325838bba7d9c75a2c7e8d764162733b (patch)
treec2ca68c4bc978fb030217540c7bec428d76ab050
parentc9f64b685b197a9469332c95042f2698635ffee0 (diff)
downloadperlweeklychallenge-club-5d7e82ad325838bba7d9c75a2c7e8d764162733b.tar.gz
perlweeklychallenge-club-5d7e82ad325838bba7d9c75a2c7e8d764162733b.tar.bz2
perlweeklychallenge-club-5d7e82ad325838bba7d9c75a2c7e8d764162733b.zip
Fix error in task 2
-rwxr-xr-xchallenge-245/wlmb/perl/ch-2.pl20
1 files changed, 12 insertions, 8 deletions
diff --git a/challenge-245/wlmb/perl/ch-2.pl b/challenge-245/wlmb/perl/ch-2.pl
index 2c1b27e2a9..2fed8c0f94 100755
--- a/challenge-245/wlmb/perl/ch-2.pl
+++ b/challenge-245/wlmb/perl/ch-2.pl
@@ -15,7 +15,7 @@ my $index=0;
my $total;
my @one;
my @two;
-my @sorted= sort{$a<=>$b}@ARGV;
+my @sorted= sort{"$a$b"<=>"$b$a"}@ARGV; # increasing order
for(@sorted){
my $residue=$_%3;
push @one, $index if $residue==1;
@@ -24,21 +24,25 @@ for(@sorted){
++$index;
}
$total%=3;
-my @candidates;
+my @candidates; # if neccesary to remove numbers
if($total==1){
- push @candidates, [$one[0]] if(@one>=1);
+ # remove the smallest number that leaves a residue one
+ push @candidates, [$one[0]] if(@one);
+ # remove the two smallest numbers that leaves a residue one
push @candidates, [@two[1,0]] if(@two>=2);
}
if($total==2){
+ # remove the two smallest numbers that leaves a residue one
push @candidates, [@one[1,0]] if(@one>=2);
- push @candidates, [$two[0]] if(@two>=1);
+ # or the smallest numbers that leaves a residue two
+ push @candidates, [$two[0]] if(@two);
}
-if(@candidates){
+if(@candidates){ # find smallest candidates
my @to_remove=map {join "", @sorted[@$_]} @candidates;
my $index_to_remove=@candidates == 2 && $to_remove[1]<$to_remove[0]?1:0;
- splice @sorted, $_, 1 for @{$candidates[$index_to_remove]};
+ splice @sorted, $_, 1 for @{$candidates[$index_to_remove]}; # remove one or two numbers
}
my $result=join "", reverse @sorted;
-$result=-1 if $result eq "";
-$result=0 if $result==0; # "0000->0";
+$result=-1 if $result eq ""; # removed all
+$result=0 if $result==0; # 0000...=0 is divisible;
say "@ARGV -> $result";