aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2021-11-08 19:06:10 -0500
committerDave Jacoby <jacoby.david@gmail.com>2021-11-08 19:06:10 -0500
commitcbf35aaac859f383ad1e74ff80a80cfcca8508f6 (patch)
treee9793e478fc91570ab2528fba94861c53a9739d4
parent38b45f2b11e7239fa265015585844cecdd9b12a8 (diff)
downloadperlweeklychallenge-club-cbf35aaac859f383ad1e74ff80a80cfcca8508f6.tar.gz
perlweeklychallenge-club-cbf35aaac859f383ad1e74ff80a80cfcca8508f6.tar.bz2
perlweeklychallenge-club-cbf35aaac859f383ad1e74ff80a80cfcca8508f6.zip
Laugh
-rw-r--r--challenge-138/blog.txt1
-rw-r--r--challenge-138/dave-jacoby/perl/ch-1.pl63
-rw-r--r--challenge-138/dave-jacoby/perl/ch-2.pl60
3 files changed, 124 insertions, 0 deletions
diff --git a/challenge-138/blog.txt b/challenge-138/blog.txt
new file mode 100644
index 0000000000..13e0a70a85
--- /dev/null
+++ b/challenge-138/blog.txt
@@ -0,0 +1 @@
+https://jacoby.github.io/2021/11/08/i-thank-you-for-the-days-the-weekly-challenge-138.html
diff --git a/challenge-138/dave-jacoby/perl/ch-1.pl b/challenge-138/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..16bd98bbd4
--- /dev/null
+++ b/challenge-138/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say state postderef signatures };
+no warnings qw{ experimental };
+
+use DateTime;
+
+for my $y ( 2000 .. 2040 ) {
+ say join "\t", $y, workdays2($y), workdays1($y);
+}
+
+# The brute force solution, where I go through each year,
+# checking if each day is a work day, and keeping count.
+
+sub workdays1 ( $year ) {
+ my $day = DateTime->new(
+ day => 1,
+ month => 1,
+ year => $year,
+ time_zone => 'floating'
+ );
+ my $c = 0;
+ while ( $year == $day->year ) {
+ $c++ if $day->day_of_week <= 5;
+ $day->add( days => 1 );
+ }
+ return $c;
+}
+
+# But there are ONLY 14 years. Leap year or not = 2. Days of week = 7.
+# 2 * 7 == 14. So it's perfectly reasonable to know that, if the year
+# is a leapyar and starts on a Saturday, or starts on a Sunday, leap year
+# or no, that's going to be a 260-workday year, and if it's a leap year
+# and starts on Monday, Tuesday, Wednesday or Thurday, there will be
+# 262, and otherwise, there will be 261.
+
+sub workdays2( $year ) {
+ my $table = {};
+ $table->{0}{1} = 261;
+ $table->{0}{2} = 261;
+ $table->{0}{3} = 261;
+ $table->{0}{4} = 261;
+ $table->{0}{5} = 261;
+ $table->{0}{6} = 260;
+ $table->{0}{7} = 260;
+ $table->{1}{1} = 262;
+ $table->{1}{2} = 262;
+ $table->{1}{3} = 262;
+ $table->{1}{4} = 262;
+ $table->{1}{5} = 261;
+ $table->{1}{6} = 260;
+ $table->{1}{7} = 261;
+ my $day = DateTime->new(
+ day => 1,
+ month => 1,
+ year => $year,
+ time_zone => 'floating'
+ );
+ return $table->{ $day->is_leap_year }{ $day->dow };
+}
+
diff --git a/challenge-138/dave-jacoby/perl/ch-2.pl b/challenge-138/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..7aa380dbe5
--- /dev/null
+++ b/challenge-138/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say postderef signatures state };
+no warnings qw{ experimental };
+
+use List::Util qw{ sum0 uniq };
+
+my @squares = map { $_**2 } 1 .. 100;
+
+for my $n (@squares) {
+ my $split = split_number($n);
+ say join "\t", '', $split, $n,;
+}
+
+sub split_number($n) {
+ my $sqrt = sqrt($n);
+ my @split = split //, $n;
+ if ( scalar @split == 1 ) {
+ my $s = shift @split;
+ return $s == $sqrt ? 1 : 0;
+ }
+ else {
+ my @numbers = break_up( 1, @split );
+ for my $num (@numbers) {
+ my $sum = sum0 split /\D/, $num;
+ return 1 if $sqrt == $sum;
+ }
+ }
+ return 0;
+}
+
+sub break_up ( $position, @array ) {
+ my @output;
+ my $len = scalar @array;
+ my @dup = @array;
+ if ( $len <= $position ) {
+ return join '+', @array;
+ }
+
+ my @copy;
+ my $i = 0;
+ while (@dup) {
+ if ( $i eq $position ) {
+ my $x = shift @dup;
+ $copy[-1] .= $x;
+ }
+ else {
+ push @copy, shift @dup;
+ }
+ $i++;
+ }
+
+ push @output, break_up( $position, @copy );
+ push @output, break_up( $position + 1, @array );
+
+ @output = uniq sort grep { defined } @output;
+ return @output;
+}