aboutsummaryrefslogtreecommitdiff
path: root/challenge-227
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-07-24 21:11:07 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-07-28 16:04:22 +0200
commit5d541be2b35dc83e0c8611bf312bfbd25e40b1fb (patch)
tree8f3333790228f54c74cdff2d4be2744e79b3dee4 /challenge-227
parentd5ce09a67599e62df79105bfe5d87dffe59bb083 (diff)
downloadperlweeklychallenge-club-5d541be2b35dc83e0c8611bf312bfbd25e40b1fb.tar.gz
perlweeklychallenge-club-5d541be2b35dc83e0c8611bf312bfbd25e40b1fb.tar.bz2
perlweeklychallenge-club-5d541be2b35dc83e0c8611bf312bfbd25e40b1fb.zip
Solution to task 1
Diffstat (limited to 'challenge-227')
-rwxr-xr-xchallenge-227/jo-37/perl/ch-1.pl72
1 files changed, 72 insertions, 0 deletions
diff --git a/challenge-227/jo-37/perl/ch-1.pl b/challenge-227/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..37edaaf088
--- /dev/null
+++ b/challenge-227/jo-37/perl/ch-1.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use Date::Manip::Recur;
+use experimental 'signatures';
+
+our ($tests, $examples, $verbose);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV == 1 && $ARGV[0] =~ /^\d{4}$/;
+usage: $0 [-examples] [-tests] [-verbose] [YYYY]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-verbose
+ enable trace output
+
+YYYY
+ find all Friday 13th for the given year
+
+EOS
+
+
+### Input and Output
+
+if ($verbose) {
+ say $_->printf('%b') for friday_13th($ARGV[0]);
+} else {
+ say scalar friday_13th($ARGV[0]);
+}
+
+
+### Implementation
+
+sub friday_13th ($year) {
+ # Select the 13th day of every month, limit the result to Fridays and
+ # apply this frequency to the given year.
+ # For some strange reason this fails for 9998 and 9999, even if the
+ # start and end dates are provided as valid Date::Manip::Date
+ # objects. Thus keeping it simple and just specify the (beginning
+ # of the) year.
+ Date::Manip::Recur->new('0:1*0:13:0:0:0*IW5')->dates($year, $year + 1);
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is scalar(friday_13th(2023)), 2, 'example';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is scalar(friday_13th(1753)), 2, '1753 succeeds';
+ is scalar(friday_13th(9997)), 1, '9997 succeeds';
+ is scalar(friday_13th(9998)), 3, '9998 fails';
+ is scalar(friday_13th(9999)), 1, '9999 fails';
+ }
+
+ done_testing;
+ exit;
+}