aboutsummaryrefslogtreecommitdiff
path: root/challenge-030
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-10-20 14:49:45 +0100
committerGitHub <noreply@github.com>2019-10-20 14:49:45 +0100
commit80f6063a67dff8b264dc19e63d2d482c81f39760 (patch)
tree6102bf0abe152df3def354a1723f8f5e2760344c /challenge-030
parent3fc00c79fee5c947fe489c012257f16d2b7552d7 (diff)
parent3e5c41cfe6ed676b4e0153bdedb21ae84e318a52 (diff)
downloadperlweeklychallenge-club-80f6063a67dff8b264dc19e63d2d482c81f39760.tar.gz
perlweeklychallenge-club-80f6063a67dff8b264dc19e63d2d482c81f39760.tar.bz2
perlweeklychallenge-club-80f6063a67dff8b264dc19e63d2d482c81f39760.zip
Merge pull request #810 from creewick/master
Solved
Diffstat (limited to 'challenge-030')
-rw-r--r--challenge-030/creewick/ch-1.pl40
-rw-r--r--challenge-030/creewick/ch-2.pl44
2 files changed, 84 insertions, 0 deletions
diff --git a/challenge-030/creewick/ch-1.pl b/challenge-030/creewick/ch-1.pl
new file mode 100644
index 0000000000..0755317e14
--- /dev/null
+++ b/challenge-030/creewick/ch-1.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use v5.10;
+
+# And you can change these constants to get the solution for day other than Christmas, or for other day of week
+
+my $currentDayOfWeek = 3;
+my $currentYear = 2019;
+my $REQUESTED_DAY_OF_WEEK = 0;
+my $DAYS_IN_WEEK = 7;
+my $DAYS_IN_YEAR = 365;
+
+sub getNextWeekDay {
+ my ($prevDay, $isLeap) = @_;
+ my $days = 365 + $isLeap;
+
+ return ($prevDay + $days) % $DAYS_IN_WEEK;
+}
+
+# And you can change the definition of the leap year, for example, to include centuries in them
+
+sub isLeapYear {
+ my ($year) = @_;
+
+ return $year % 4 > 0
+ ? 0
+ : $year % 100 > 0
+ ? 1
+ : $year % 400 == 0;
+}
+
+while ($currentYear < 2100) {
+ my $isLeap = isLeapYear(++$currentYear);
+ $currentDayOfWeek = getNextWeekDay($currentDayOfWeek, $isLeap);
+
+ if ($currentDayOfWeek == $REQUESTED_DAY_OF_WEEK) {
+ say $currentYear;
+ }
+} \ No newline at end of file
diff --git a/challenge-030/creewick/ch-2.pl b/challenge-030/creewick/ch-2.pl
new file mode 100644
index 0000000000..65aadf55b8
--- /dev/null
+++ b/challenge-030/creewick/ch-2.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use v5.10;
+
+# Here you can vary the numbers count in series and the required sum
+
+my $REQUIRED_SUM = 12;
+my $NUMBERS_COUNT = 3;
+
+# And define whether current series fits the conditions or not
+
+sub isGoodSeries {
+ my (@numbers) = @_;
+ my ($sum, $evensCount) = (0, 0);
+
+ foreach (@numbers) {
+ $sum += $_;
+ if ($_ % 2 == 0) {
+ $evensCount++;
+ }
+ }
+
+ return $sum == $REQUIRED_SUM && $evensCount > 0;
+}
+
+sub checkAllSeries {
+ my @series = @_;
+ my $count = @series;
+
+ if ($count == $NUMBERS_COUNT) {
+ if (isGoodSeries(@series)) {
+ say "@series";
+ }
+ } else {
+ for my $i (1..$REQUIRED_SUM) {
+ my @newSeries = @series;
+ push(@newSeries, $i);
+ checkAllSeries(@newSeries);
+ }
+ }
+}
+
+checkAllSeries(()); \ No newline at end of file