aboutsummaryrefslogtreecommitdiff
path: root/challenge-138/james-smith/perl/ch-1.pl
blob: 82d1ad9fe85a1030ff634b66aaf58d4f7cb69e29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/local/bin/perl

use strict;

use warnings;
use feature qw(say);
use Test::More;
use Benchmark qw(cmpthese timethis);
use Data::Dumper qw(Dumper);

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( work_days($_->[0]), $_->[1] ) foreach @TESTS;

done_testing();

#say $_, ' ', ly($_), ' ', zf($_), ' ', workdays($_) foreach 1900..2100;

sub leap_year {
  $_[0]&3 || (!($_[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 work_days {
  260 + $EXTRA_WORKDAYS[ leap_year $_[0] ][ zellers_congruence_jan_1 $_[0]-1 ];
}