From 152e62ab49632f34b9b8f642fe893d80115922bb Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sun, 4 Aug 2019 13:26:26 -0600 Subject: Solution to 19.1 in P5 and P6 --- challenge-019/joelle-maslak/perl5/ch-1.pl | 32 +++++++++++++++++++++++++++++++ challenge-019/joelle-maslak/perl6/ch-1.p6 | 29 ++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100755 challenge-019/joelle-maslak/perl5/ch-1.pl create mode 100755 challenge-019/joelle-maslak/perl6/ch-1.p6 diff --git a/challenge-019/joelle-maslak/perl5/ch-1.pl b/challenge-019/joelle-maslak/perl5/ch-1.pl new file mode 100755 index 0000000000..35e12be63a --- /dev/null +++ b/challenge-019/joelle-maslak/perl5/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use v5.22; + +# Turn on method signatures +use feature 'signatures'; +no warnings 'experimental::signatures'; + +use POSIX qw(mktime strftime); + +my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); + +my (@days) = map { get_all_days($_)->@* } 1900..2019; +say join "\n", + map { $months[(gmtime($_))[4]] . " " . strftime("%Y", gmtime($_)) } + grep { (gmtime($_))[6] == 0 and (gmtime($_))[3] == 31 } + @days; + +sub get_all_days($year) { + my @ret; + my $day = mktime(0,0,12,1,0,$year-1900); + while ((gmtime($day))[5] == $year-1900) { + push @ret, $day; + $day += 86400; # Number of seconds in a day + } + + return \@ret; +} + diff --git a/challenge-019/joelle-maslak/perl6/ch-1.p6 b/challenge-019/joelle-maslak/perl6/ch-1.p6 new file mode 100755 index 0000000000..330788a6e6 --- /dev/null +++ b/challenge-019/joelle-maslak/perl6/ch-1.p6 @@ -0,0 +1,29 @@ +#!/usr/bin/env perl6 +use v6; + +# Shortcut: to have 5 weekends (including Friday), you need 31 days, and +# the 31st must fall on a Sunday. + +my @months = « + invalid + Jan Feb Mar Apr May Jun + Jul Aug Sep Oct Nov Dec +»; + +sub MAIN() { + my $days = (1900..2019).map( { get-all-days($_) } ).flat; + my $sunday31st = $days.grep( { $_.day-of-week == 7 and $_.day-of-month == 31 } ); + say $sunday31st.map( { @months[$_.month] ~ " " ~ $_.year } ).join("\n"); +} + +sub get-all-days(UInt:D $year) { + return gather { + state $dt = Date.new($year, 1, 1); + while $dt.year == $year { + take $dt; + $dt = $dt.succ; + } + } +} + + -- cgit