The weekly challenge 227 - Task 1: Friday 13th

 1 #!/usr/bin/perl
 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-227/#TASK1
 3 #
 4 # Task 1: Friday 13th
 5 # ===================
 6 #
 7 # You are given a year number in the range 1753 to 9999.
 8 #
 9 # Write a script to find out how many dates in the year are Friday 13th, assume that the current Gregorian calendar applies.
10 #
11 ## Example
12 ##
13 ## Input: $year = 2023
14 ## Output: 2
15 ##
16 ## Since there are only 2 Friday 13th in the given year 2023 i.e. 13th Jan and 13th Oct.
17 #
18 ############################################################
19 ##
20 ## discussion
21 ##
22 ############################################################
23 #
24 # This is a classic "use a module from CPAN" problem.
25 # DateTime lets us do all we need here very simple.
26 # Just create a new DateTime object for the 13th of each
27 # month of the given year and check whether its weekday is
28 # Friday.
29 
30 use strict;
31 use warnings;
32 use DateTime;
33 
34 friday_13th(2023);
35 
36 sub friday_13th {
37    my $year = shift;
38    print "Input: \$year = $year\n";
39    my $count = 0;
40    foreach my $month (1..12) {
41       my $dt = DateTime->new(
42          year => $year,
43          month => $month,
44          day => 13
45       );
46       $count++ if $dt->day_of_week == 5;
47    }
48    print "Output: $count\n";
49 }
50