aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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