aboutsummaryrefslogtreecommitdiff
path: root/challenge-187/sgreen
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2022-10-24 17:44:04 +0800
committer冯昶 <fengchang@novel-supertv.com>2022-10-24 17:44:04 +0800
commit7783ecb7dad2ae5ea3d3022a13e3901c196c4154 (patch)
treefa408d0b19c541602d81799c8a13eea61df2cb8f /challenge-187/sgreen
parenta406c6935f88c7afa04a707a2611ebe4fe4eadae (diff)
parentbb06570f6b1634ca14bfd08927f3bfc6c052c494 (diff)
downloadperlweeklychallenge-club-7783ecb7dad2ae5ea3d3022a13e3901c196c4154.tar.gz
perlweeklychallenge-club-7783ecb7dad2ae5ea3d3022a13e3901c196c4154.tar.bz2
perlweeklychallenge-club-7783ecb7dad2ae5ea3d3022a13e3901c196c4154.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-187/sgreen')
-rw-r--r--challenge-187/sgreen/README.md4
-rw-r--r--challenge-187/sgreen/blog.txt1
-rwxr-xr-xchallenge-187/sgreen/perl/ch-1.pl50
-rwxr-xr-xchallenge-187/sgreen/perl/ch-2.pl33
-rwxr-xr-xchallenge-187/sgreen/python/ch-1.py43
-rwxr-xr-xchallenge-187/sgreen/python/ch-2.py28
6 files changed, 157 insertions, 2 deletions
diff --git a/challenge-187/sgreen/README.md b/challenge-187/sgreen/README.md
index dab33e5312..886789d657 100644
--- a/challenge-187/sgreen/README.md
+++ b/challenge-187/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 183
+# The Weekly Challenge 187
-Blog: [Unique differences](https://dev.to/simongreennet/unique-differences-1cci)
+Blog: [Magical Days Together with Triplets](https://dev.to/simongreennet/magical-days-together-with-triplets-3lci)
diff --git a/challenge-187/sgreen/blog.txt b/challenge-187/sgreen/blog.txt
new file mode 100644
index 0000000000..3b355771ad
--- /dev/null
+++ b/challenge-187/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/magical-days-together-with-triplets-3lci \ No newline at end of file
diff --git a/challenge-187/sgreen/perl/ch-1.pl b/challenge-187/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..3d7dd79cd8
--- /dev/null
+++ b/challenge-187/sgreen/perl/ch-1.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util qw(min max);
+use Date::Calc qw(Day_of_Year);
+
+sub get_dofy ($date) {
+ # Turn a date in DD-MM format into day of year'''
+ my ( $day, $month ) = split( /-/, $date );
+ return Day_of_Year( 2022, $month, $day );
+}
+
+sub main (@array) {
+ #Calculate the days that foo and bar spent together
+
+ # Convert the input into the day of year
+ my $foo_start = get_dofy( $array[0] );
+ my $foo_end = get_dofy( $array[1] );
+ my $bar_start = get_dofy( $array[2] );
+ my $bar_end = get_dofy( $array[3] );
+
+ # Make sure they arrive before they leave!
+ if ( $foo_end < $foo_start ) {
+ die "foo left before they arrived!\n";
+ }
+ if ( $bar_end < $bar_start ) {
+ die "bar left before they arrived!\n";
+ }
+
+# The time they spent together is when the last person arrives and the first one leave.
+ my $both_start = max( $foo_start, $bar_start );
+ my $both_end = min( $foo_end, $bar_end );
+
+ # Display the days spent together
+ if ( $both_end < $both_start ) {
+ say '0 days';
+ }
+ elsif ( $both_start == $both_end ) {
+ say '1 day';
+ }
+ else {
+ say +( $both_end - $both_start + 1 ), ' days';
+ }
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-187/sgreen/perl/ch-2.pl b/challenge-187/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..d5dfac9906
--- /dev/null
+++ b/challenge-187/sgreen/perl/ch-2.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use Algorithm::Combinatorics 'combinations';
+use List::Util 'sum';
+
+sub main (@array) {
+ my $solution = [];
+
+ # Calculate for all combinations
+ my $combinations = combinations( \@array, 3 );
+ while ( my $new = $combinations->next ) {
+ my ( $x, $y, $z ) = @$new;
+ if ( $x + $y > $z and $y + $z > $x and $x + $z > $y ) {
+ if ( scalar(@$solution) == 0 or sum(@$new) > sum(@$solution) ) {
+ $solution = [ sort { $b <=> $a } @$new ];
+ }
+ }
+ }
+
+ if ( scalar(@$solution) == 0 ) {
+ say '()';
+ }
+ else {
+ say '(', join( ', ', @$solution ), ')';
+ }
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-187/sgreen/python/ch-1.py b/challenge-187/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..5aa9ac45b7
--- /dev/null
+++ b/challenge-187/sgreen/python/ch-1.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+
+import sys
+from datetime import date
+
+
+def get_dofy(dt):
+ '''Turn a date in DD-MM format into day of year'''
+ (day, month) = dt.split('-')
+ d = date(2022, int(month), int(day))
+ return int(d.strftime('%j'))
+
+
+def main(array):
+ '''Calculate the days that foo and bar spent together'''
+
+ # Convert the input into the day of year
+ foo_start = get_dofy(array[0])
+ foo_end = get_dofy(array[1])
+ bar_start = get_dofy(array[2])
+ bar_end = get_dofy(array[3])
+
+ # Make sure they arrive before they leave!
+ if foo_end < foo_start:
+ raise ValueError('foo left before they arrived!')
+ if bar_end < bar_start:
+ raise ValueError('bar left before they arrived!')
+
+ # The time they spent together is when the last person arrives and the first one leave.
+ both_start = max(foo_start, bar_start)
+ both_end = min(foo_end, bar_end)
+
+ # Display the days spent together
+ if both_end < both_start:
+ print('0 days')
+ elif both_start == both_end:
+ print('1 day')
+ else:
+ print(f'{both_end - both_start + 1} days')
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:5])
diff --git a/challenge-187/sgreen/python/ch-2.py b/challenge-187/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..e76921c374
--- /dev/null
+++ b/challenge-187/sgreen/python/ch-2.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python3
+
+import sys
+from decimal import Decimal
+from itertools import combinations
+
+
+def main(array):
+ solution = None
+
+ # Turn the array into integers
+ n = [Decimal(x) for x in array]
+
+ # Calculate for all combinations
+ for new in combinations(n, 3):
+ (a, b, c) = new
+ if a + b > c and b + c > a and a + c > b:
+ if solution is None or sum(new) > sum(solution):
+ solution = sorted(new, reverse = True)
+
+ if solution:
+ print('(' + ', '.join(str(x) for x in solution) + ')')
+ else:
+ print('()')
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])