From 837e9742b398b0d03fe415cc4edae1f6898fcabb Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Tue, 3 Dec 2019 00:09:39 +0100 Subject: Add solutions to 037 (Workday count + Daylight loss) by E. Choroba --- challenge-037/e-choroba/perl5/ch-1.pl | 33 ++++++++++++++++++++++++++++++ challenge-037/e-choroba/perl5/ch-2.pl | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100755 challenge-037/e-choroba/perl5/ch-1.pl create mode 100755 challenge-037/e-choroba/perl5/ch-2.pl diff --git a/challenge-037/e-choroba/perl5/ch-1.pl b/challenge-037/e-choroba/perl5/ch-1.pl new file mode 100755 index 0000000000..9369afe213 --- /dev/null +++ b/challenge-037/e-choroba/perl5/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use Time::Piece; +use Time::Seconds qw{ ONE_DAY }; + +sub days_in_month { + my ($month) = @_; + my $date = 'Time::Piece'->strptime("2019 $month 1 12:00", + '%Y %b %d %H:%M'); + my $count = 0; + while ($date->month eq $month) { + ++$count unless grep $date->day eq $_, qw( Sat Sun ); + $date += ONE_DAY; + } + return $count +} + +use Test::More tests => 12; + +is days_in_month('Jan'), 23, 'Jan'; +is days_in_month('Feb'), 20, 'Feb'; +is days_in_month('Mar'), 21, 'Mar'; +is days_in_month('Apr'), 22, 'Apr'; +is days_in_month('May'), 23, 'May'; +is days_in_month('Jun'), 20, 'Jun'; +is days_in_month('Jul'), 23, 'Jul'; +is days_in_month('Aug'), 22, 'Aug'; +is days_in_month('Sep'), 21, 'Sep'; +is days_in_month('Oct'), 23, 'Oct'; +is days_in_month('Nov'), 21, 'Nov'; +is days_in_month('Dec'), 22, 'Dec'; diff --git a/challenge-037/e-choroba/perl5/ch-2.pl b/challenge-037/e-choroba/perl5/ch-2.pl new file mode 100755 index 0000000000..83ac8e0cdb --- /dev/null +++ b/challenge-037/e-choroba/perl5/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +use Syntax::Construct qw{ /r }; + +use WWW::Mechanize; +use HTML::TableExtract; + +my $url_template + = 'https://www.timeanddate.com/sun/uk/london?month=%m%&year=2019'; +my $mech = 'WWW::Mechanize'->new; + +my %seconds; +for my $month (11, 12) { + my $url = $url_template =~ s/%m%/$month/r; + $mech->get($url); + my $te = 'HTML::TableExtract'->new; + $te->parse($mech->content); + my $table = $te->first_table_found; + for ($table->rows) { + my $time = $_->[3] or next; + + my ($h, $m, $s) = $time =~ /^([0-9]+):([0-9]+):([0-9]+)$/ + or next; + $s += $m * 60 + $h * 60 * 60; + $seconds{$month} += $s; + } +} + +my $s = $seconds{11} - $seconds{12}; +my $seconds = $s % 60; +my $m = ($s - $seconds) / 60; +my $minutes = $m % 60; +my $hours = ($m - $minutes) / 60; + +say sprintf "%d seconds or %d:%02d:%02d", $s, $hours, $minutes, $seconds; -- cgit