aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-138/james-smith/perl/ch-1.pl17
-rw-r--r--challenge-138/james-smith/perl/ch-2.pl10
2 files changed, 14 insertions, 13 deletions
diff --git a/challenge-138/james-smith/perl/ch-1.pl b/challenge-138/james-smith/perl/ch-1.pl
index 6f6fdd0979..82d1ad9fe8 100644
--- a/challenge-138/james-smith/perl/ch-1.pl
+++ b/challenge-138/james-smith/perl/ch-1.pl
@@ -8,27 +8,26 @@ use Test::More;
use Benchmark qw(cmpthese timethis);
use Data::Dumper qw(Dumper);
-my @EXTRA = ( [qw(0 1 1 1 1 1 0)], [qw(1 2 2 2 2 1 0)] );
+my @EXTRA_WORKDAYS = ( [qw(0 1 1 1 1 1 0)], [qw(1 2 2 2 2 1 0)] );
my @TESTS = (
[ 2021, 261 ],
[ 2020, 262 ],
);
-is( workdays($_->[0]), $_->[1] ) foreach @TESTS;
+is( work_days($_->[0]), $_->[1] ) foreach @TESTS;
done_testing();
#say $_, ' ', ly($_), ' ', zf($_), ' ', workdays($_) foreach 1900..2100;
-sub workdays {
- 260 + $EXTRA[ ly($_[0]) ][ zf($_[0]) ];
+sub leap_year {
+ $_[0]&3 || (!($_[0]%100) && $_[0]%400) ? 0 : 1;
}
-
-sub ly {
- $_[0]%4 || (!($_[0]%100) && $_[0]%400) ? 0 : 1;
+sub zellers_congruence_jan_1 {
+ ( 1 + $_[0]%100 + ($_[0]%100>>2) - ($_[0]/100<<1) + ($_[0]/400>>0) ) % 7;
}
-sub zf {
- ( 1 + (my$y=$_[0]-1)%100 + ($y%100>>2) + ($y/400<<0) - ($y/100<<1) )%7;
+sub work_days {
+ 260 + $EXTRA_WORKDAYS[ leap_year $_[0] ][ zellers_congruence_jan_1 $_[0]-1 ];
}
diff --git a/challenge-138/james-smith/perl/ch-2.pl b/challenge-138/james-smith/perl/ch-2.pl
index e1b04c63e7..44d447b321 100644
--- a/challenge-138/james-smith/perl/ch-2.pl
+++ b/challenge-138/james-smith/perl/ch-2.pl
@@ -19,14 +19,16 @@ is( check_square($_->[0]), $_->[1] ) foreach @TESTS;
done_testing();
sub check_square {
- return split_no( sqrt($_[0]), $_[0], 0 );
+ $_[0] <= 1 ? 0 : split_no( sqrt($_[0]), $_[0], 0 );
}
sub split_no {
my( $sum, $str ) = @_;
- return $sum?0:1 if $str eq '';
- return 0 if $sum<0;
- return 1 if grep { split_no( ($sum - substr $str,$_) , substr $str, 0, $_ ) } 0 .. -1 + length $str;
+ return 0 if $sum < 0;
+ return 0 if $str eq '' && $sum;
+ return 1 if $str eq '';
+ return 1 if grep { split_no( ($sum - substr $str,$_) , substr $str, 0, $_ ) }
+ 0 .. -1 + length $str;
return 0;
}