From 88ecae132ff26f720574b1f8ada9e1c34c1bc4e8 Mon Sep 17 00:00:00 2001 From: creewick Date: Sun, 20 Oct 2019 18:43:41 +0500 Subject: Solved --- challenge-030/creewick/task1.pl | 40 +++++++++++++++++++++++++++++++++++++ challenge-030/creewick/task2.pl | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 challenge-030/creewick/task1.pl create mode 100644 challenge-030/creewick/task2.pl diff --git a/challenge-030/creewick/task1.pl b/challenge-030/creewick/task1.pl new file mode 100644 index 0000000000..0755317e14 --- /dev/null +++ b/challenge-030/creewick/task1.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/task2.pl b/challenge-030/creewick/task2.pl new file mode 100644 index 0000000000..65aadf55b8 --- /dev/null +++ b/challenge-030/creewick/task2.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 -- cgit From 3e5c41cfe6ed676b4e0153bdedb21ae84e318a52 Mon Sep 17 00:00:00 2001 From: creewick Date: Sun, 20 Oct 2019 18:46:42 +0500 Subject: rename files --- challenge-030/creewick/ch-1.pl | 40 +++++++++++++++++++++++++++++++++++++ challenge-030/creewick/ch-2.pl | 44 +++++++++++++++++++++++++++++++++++++++++ challenge-030/creewick/task1.pl | 40 ------------------------------------- challenge-030/creewick/task2.pl | 44 ----------------------------------------- 4 files changed, 84 insertions(+), 84 deletions(-) create mode 100644 challenge-030/creewick/ch-1.pl create mode 100644 challenge-030/creewick/ch-2.pl delete mode 100644 challenge-030/creewick/task1.pl delete mode 100644 challenge-030/creewick/task2.pl 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 diff --git a/challenge-030/creewick/task1.pl b/challenge-030/creewick/task1.pl deleted file mode 100644 index 0755317e14..0000000000 --- a/challenge-030/creewick/task1.pl +++ /dev/null @@ -1,40 +0,0 @@ -#!/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/task2.pl b/challenge-030/creewick/task2.pl deleted file mode 100644 index 65aadf55b8..0000000000 --- a/challenge-030/creewick/task2.pl +++ /dev/null @@ -1,44 +0,0 @@ -#!/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 -- cgit