From 5d7e82ad325838bba7d9c75a2c7e8d764162733b Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Sun, 3 Dec 2023 16:42:33 -0600 Subject: Fix error in task 2 --- challenge-245/wlmb/perl/ch-2.pl | 20 ++++++++++++-------- 1 file 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"; -- cgit From 725bd5ff4373b83bc040e582f000773a33b92d78 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Sun, 3 Dec 2023 16:46:10 -0600 Subject: Fix typo --- challenge-245/wlmb/perl/ch-2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-245/wlmb/perl/ch-2.pl b/challenge-245/wlmb/perl/ch-2.pl index 2fed8c0f94..8ee193d592 100755 --- a/challenge-245/wlmb/perl/ch-2.pl +++ b/challenge-245/wlmb/perl/ch-2.pl @@ -28,7 +28,7 @@ my @candidates; # if neccesary to remove numbers if($total==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 + # remove the two smallest numbers that leaves a residue two push @candidates, [@two[1,0]] if(@two>=2); } if($total==2){ -- cgit