aboutsummaryrefslogtreecommitdiff
path: root/challenge-138
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-15 08:07:06 +0000
committerGitHub <noreply@github.com>2021-11-15 08:07:06 +0000
commit25353cecdeecc24086f4e4b0c4b715166ead4db8 (patch)
tree2a978d02d63b7607761c3fa892978cf30df0d57c /challenge-138
parent577a6d364e4b44f723fda78f47529130a77ef600 (diff)
parentc2a780bea6839d4781ff43c124494b86f0cb4f4e (diff)
downloadperlweeklychallenge-club-25353cecdeecc24086f4e4b0c4b715166ead4db8.tar.gz
perlweeklychallenge-club-25353cecdeecc24086f4e4b0c4b715166ead4db8.tar.bz2
perlweeklychallenge-club-25353cecdeecc24086f4e4b0c4b715166ead4db8.zip
Merge pull request #5223 from jaldhar/challenge-138
Challenge 138 by Jaldhar H. Vyas.
Diffstat (limited to 'challenge-138')
-rw-r--r--challenge-138/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-138/jaldhar-h-vyas/perl/ch-1.pl28
-rwxr-xr-xchallenge-138/jaldhar-h-vyas/perl/ch-2.pl79
-rwxr-xr-xchallenge-138/jaldhar-h-vyas/raku/ch-1.raku24
-rwxr-xr-xchallenge-138/jaldhar-h-vyas/raku/ch-2.raku23
5 files changed, 155 insertions, 0 deletions
diff --git a/challenge-138/jaldhar-h-vyas/blog.txt b/challenge-138/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..99d82d89f8
--- /dev/null
+++ b/challenge-138/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2021/11/perl_weekly_challenge_week_138.html
diff --git a/challenge-138/jaldhar-h-vyas/perl/ch-1.pl b/challenge-138/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..7a14c10260
--- /dev/null
+++ b/challenge-138/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+use 5.020;
+use warnings;
+
+sub p {
+ my ($year) = @_;
+ return (($year + int($year / 4) - int($year / 100) + int($year / 400)) % 7);
+}
+
+sub isLeap {
+ my ($year) = @_;
+ return $year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0);
+}
+
+my $year = shift // die "Need a year\n";
+
+my $workDays = 260;
+my $firstDay = p($year);
+
+if ($firstDay > 0 && $firstDay < 6) {
+ $workDays++;
+}
+
+if (isLeap($year) && $firstDay != 1) {
+ $workDays++;
+}
+
+say $workDays;
diff --git a/challenge-138/jaldhar-h-vyas/perl/ch-2.pl b/challenge-138/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..afa63c87ca
--- /dev/null
+++ b/challenge-138/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,79 @@
+#!/usr/bin/perl
+use 5.020;
+use warnings;
+
+sub combinations {
+ my @list = @{$_[0]};
+ my $length = $_[1];
+
+ if ($length <= 1) {
+ return map [$_], @list;
+ }
+
+ my @combos;
+
+ for (my $i = 0; $i + $length <= scalar @list; $i++) {
+ my $val = $list[$i];
+ my @rest = @list[$i + 1 .. $#list];
+ for my $c (combinations(\@rest, $length - 1)) {
+ push @combos, [$val, @{$c}] ;
+ }
+ }
+
+ return @combos;
+}
+
+sub sum {
+ my ($arr) = @_;
+ my $total = 0;
+
+ for my $elem (@{$arr}) {
+ $total += $elem;
+ }
+
+ return $total;
+}
+
+sub xx {
+ my ($str, $n) = @_;
+ return (split //, ($str x $n));
+}
+
+sub Ztilde {
+ my @a = @{ $_[0] };
+ my @b = @{ $_[1] };
+
+ my @result;
+ for my $i (0 .. scalar @b - 1) {
+ push @result, $a[$i], $b[$i];
+ }
+ return @result;
+}
+
+my $n = shift // die "Need a square number\n";
+
+my $squareRoot = sqrt $n;
+
+my $separated = join q{}, Ztilde([split //, $n], [xx('-', length($n) - 1)]);
+$separated .= substr $n, -1, 1;
+
+for my $len (0 .. length($n) - 1) {
+ for my $positions (combinations([0 .. length($n) - 1], $len)) {
+ my $s = $separated;
+
+ for my $i (@{$positions}) {
+ if ($i != 0) {
+ substr $s, 2 * $i - 1, 1, q{};
+ }
+ }
+
+ my @parts = split '-', $s;
+ if (sum(\@parts) == $squareRoot) {
+ say 1;
+ exit;
+ }
+
+ }
+}
+
+say 0;
diff --git a/challenge-138/jaldhar-h-vyas/raku/ch-1.raku b/challenge-138/jaldhar-h-vyas/raku/ch-1.raku
new file mode 100755
index 0000000000..360d82e183
--- /dev/null
+++ b/challenge-138/jaldhar-h-vyas/raku/ch-1.raku
@@ -0,0 +1,24 @@
+#!/usr/bin/raku
+
+sub p(Int $year) {
+ return (($year + ($year div 4) - ($year div 100) + ($year div 400)) % 7);
+}
+
+sub isLeap(Int $year) {
+ return $year %% 4 && ($year !%% 100 || $year %% 400);
+}
+
+sub MAIN(Int $year) {
+ my $workDays = 260;
+ my $firstDay = p($year);
+
+ if $firstDay ~~ 1 .. 5 {
+ $workDays++;
+ }
+
+ if isLeap($year) && $firstDay != 1 {
+ $workDays++;
+ }
+
+ say $workDays;
+}
diff --git a/challenge-138/jaldhar-h-vyas/raku/ch-2.raku b/challenge-138/jaldhar-h-vyas/raku/ch-2.raku
new file mode 100755
index 0000000000..f7bc19953d
--- /dev/null
+++ b/challenge-138/jaldhar-h-vyas/raku/ch-2.raku
@@ -0,0 +1,23 @@
+#!/usr/bin/raku
+
+sub MAIN(Int $n) {
+ my $squareRoot = $n.sqrt;
+
+ my $separated = ($n.comb Z~ ('-' xx $n.chars - 1)).join ~ $n.substr(*-1, 1);
+
+ for (1 .. $n.chars - 1).combinations -> @positions {
+ my $s = $separated;
+
+ for @positions -> $i {
+ $s.substr-rw(2 * $i - 1, 1) = q{};
+ }
+
+ my @parts = $s.split('-');
+ if ([+] @parts) == $squareRoot {
+ say 1;
+ exit;
+ }
+ }
+
+ say 0;
+}