aboutsummaryrefslogtreecommitdiff
path: root/challenge-030/creewick/perl5/ch-1.pl
blob: 0755317e14fc5278fff4b02992fd5d6b3bbf8774 (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
34
35
36
37
38
39
40
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;

# And you can change these constants to get the solution for day other than Christmas, or for other day of week

my $currentDayOfWeek = 3;
my $currentYear = 2019;
my $REQUESTED_DAY_OF_WEEK = 0;
my $DAYS_IN_WEEK = 7;
my $DAYS_IN_YEAR = 365;

sub getNextWeekDay {
    my ($prevDay, $isLeap) = @_;
    my $days = 365 + $isLeap;

    return ($prevDay + $days) % $DAYS_IN_WEEK;
}

# And you can change the definition of the leap year, for example, to include centuries in them

sub isLeapYear {
    my ($year) = @_;

    return $year % 4 > 0
        ? 0
        : $year % 100 > 0
            ? 1
            : $year % 400 == 0;
}

while ($currentYear < 2100) {
    my $isLeap = isLeapYear(++$currentYear);
    $currentDayOfWeek = getNextWeekDay($currentDayOfWeek, $isLeap);

    if ($currentDayOfWeek == $REQUESTED_DAY_OF_WEEK) {
       say $currentYear;
    }
}