aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-13 00:27:56 +0000
committerGitHub <noreply@github.com>2021-11-13 00:27:56 +0000
commitdfd902f866991ac144720168257a3b2334f75719 (patch)
treeee42a9380466b7ea1a26845748dc2b83cc1f67e5
parent901897526036bcf8cf8c270cd624c20a5df5bf9c (diff)
parent93a9201d14d6a29337825c1dba65626e343bd51c (diff)
downloadperlweeklychallenge-club-dfd902f866991ac144720168257a3b2334f75719.tar.gz
perlweeklychallenge-club-dfd902f866991ac144720168257a3b2334f75719.tar.bz2
perlweeklychallenge-club-dfd902f866991ac144720168257a3b2334f75719.zip
Merge pull request #5204 from polettix/polettix/pwc138
Add polettix's solution to challenge-138
-rw-r--r--challenge-138/polettix/blog.txt1
-rw-r--r--challenge-138/polettix/blog1.txt1
-rw-r--r--challenge-138/polettix/perl/ch-1.pl18
-rw-r--r--challenge-138/polettix/perl/ch-2.pl30
-rw-r--r--challenge-138/polettix/raku/ch-1.raku13
-rw-r--r--challenge-138/polettix/raku/ch-2.raku30
6 files changed, 93 insertions, 0 deletions
diff --git a/challenge-138/polettix/blog.txt b/challenge-138/polettix/blog.txt
new file mode 100644
index 0000000000..191cd044cf
--- /dev/null
+++ b/challenge-138/polettix/blog.txt
@@ -0,0 +1 @@
+https://github.polettix.it/ETOOBUSY/2021/11/10/pwc138-workdays/
diff --git a/challenge-138/polettix/blog1.txt b/challenge-138/polettix/blog1.txt
new file mode 100644
index 0000000000..95e3f4748b
--- /dev/null
+++ b/challenge-138/polettix/blog1.txt
@@ -0,0 +1 @@
+https://github.polettix.it/ETOOBUSY/2021/11/11/pwc138-split-number/
diff --git a/challenge-138/polettix/perl/ch-1.pl b/challenge-138/polettix/perl/ch-1.pl
new file mode 100644
index 0000000000..047ebdc64c
--- /dev/null
+++ b/challenge-138/polettix/perl/ch-1.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/env perl
+use v5.24;
+use warnings;
+use experimental 'signatures';
+no warnings 'experimental::signatures';
+use Time::Local 'timegm';
+use List::Util qw< min max >;
+
+sub dow ($y, $m, $d) { (gmtime(timegm(1, 1, 1, $d, --$m, $y)))[6] }
+sub workdays ($y) {
+ my $bdow = dow($y, 1, 1);
+ my $edow = dow($y, 12, 31);
+ my $bdays = 8 - $bdow; # 1 - 7
+ my $ydays = 365 + ($bdow == $edow ? 0 : 1) - $bdays - $edow;
+ return max($bdays - 2, 0) + ($ydays / 7 * 5) + min($edow, 5);
+}
+
+say workdays(shift // 2021);
diff --git a/challenge-138/polettix/perl/ch-2.pl b/challenge-138/polettix/perl/ch-2.pl
new file mode 100644
index 0000000000..63eeffdbdf
--- /dev/null
+++ b/challenge-138/polettix/perl/ch-2.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+use v5.24;
+use warnings;
+use experimental 'signatures';
+no warnings 'experimental::signatures';
+use List::Util 'sum';
+
+sub split_number ($square) {
+ my $root = int sqrt $square;
+ die "invalid input <$square>: not a square"
+ unless $root * $root == $square;
+ return 1 if $root =~ m{\A 9+ \z}mxs;
+ my ($first, @digits) = split m{}mxs, $square;
+ my $n_separators = @digits;
+ for my $i (1 .. 2**$n_separators - 1) {
+ my @split = $first;
+ my @separators = split m{}mxs, sprintf "%0${n_separators}b", $i;
+ for my $j (0 .. $#separators) {
+ if ($separators[$j]) { push @split, $digits[$j] }
+ else { $split[-1] .= $digits[$j] }
+ }
+ return 1 if sum(@split) == $root;
+ } ## end for my $i (1 .. 2**$n_separators...)
+ return 0;
+} ## end sub split_number ($square)
+
+if (@ARGV) { say split_number($ARGV[0]) }
+else {
+ split_number($_ * $_) && say $_*$_ for 1 .. 1000;
+}
diff --git a/challenge-138/polettix/raku/ch-1.raku b/challenge-138/polettix/raku/ch-1.raku
new file mode 100644
index 0000000000..7f7b9b2230
--- /dev/null
+++ b/challenge-138/polettix/raku/ch-1.raku
@@ -0,0 +1,13 @@
+#!/usr/bin/env raku
+use v6;
+
+subset FullyGregorianYear of Int where * > 1582;
+sub workdays (FullyGregorianYear $y) {
+ my $bdow = Date.new($y, 1, 1).day-of-week;
+ my $edow = Date.new($y, 12, 31).day-of-week;
+ my $bdays = 8 - $bdow; # 1 - 7
+ my $ydays = 365 + ($bdow == $edow ?? 0 !! 1) - $bdays - $edow;
+ return max($bdays - 2, 0) + ($ydays / 7 * 5).Int + min($edow, 5);
+}
+
+sub MAIN (FullyGregorianYear $y = 2021) { workdays($y).put }
diff --git a/challenge-138/polettix/raku/ch-2.raku b/challenge-138/polettix/raku/ch-2.raku
new file mode 100644
index 0000000000..9718959c1b
--- /dev/null
+++ b/challenge-138/polettix/raku/ch-2.raku
@@ -0,0 +1,30 @@
+#!/usr/bin/env raku
+use v6;
+
+sub split-number (Int:D $square) {
+ my $root = $square.sqrt.Int;
+ die "invalid input <$square>: not a square"
+ unless $root * $root == $square;
+ return 1 if $root ~~ /^ 9+ $/;
+ my ($first, @digits) = $square.comb: /./;
+ my $n-separators = @digits.elems;
+ for 1 ..^ 2**$n-separators -> $i {
+ my @split = $first,;
+ my @separators = $i.base(2).comb: /./;
+ @separators.unshift: 0 while @separators.elems < $n-separators;
+ for 0 .. @separators.end -> $j {
+ if @separators[$j] > 0 { @split.push: @digits[$j] }
+ else { @split[*-1] ~= @digits[$j] }
+ }
+ return 1 if @split.sum == $root;
+ } ## end for my $i (1 .. 2**$n_separators...)
+ return 0;
+}
+
+sub MAIN (*@args) {
+ if @args { split-number(@args[0]).put }
+ else {
+ my @sequence = (1...Inf).map({$_**2}).grep({split-number($_) > 0});
+ @sequence[0..^10].join(', ').put;
+ }
+}