From daecb450fef6e0d653f7c6d49cb85d9ea670826c Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 17 Oct 2022 09:45:10 +0200 Subject: Task 1 done --- challenge-187/luca-ferrari/raku/ch-1.p6 | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 challenge-187/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-187/luca-ferrari/raku/ch-1.p6 b/challenge-187/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..4f795ef3c8 --- /dev/null +++ b/challenge-187/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,24 @@ +#!raku + +# Perl Weekly Challenge 187 + +sub MAIN( Str $start-foo, Str $end-foo, Str $start-bar, Str $end-bar ) { + + $start-foo ~~ / ^ ( \d ** 2 ) '-' ( \d ** 2 ) $ /; + my $foo-days-start = DateTime.new: year => 2022, day => $/[0], month => $/[1]; + $end-foo ~~ / ^ (\d ** 2) '-' (\d **2) $ /; + my $foo-days-end = DateTime.new: year => 2022, day => $/[0], month => $/[1]; + + $start-bar ~~ / ^ ( \d ** 2 ) '-' ( \d ** 2 ) $ /; + my $bar-days-start = DateTime.new: year => 2022, day => $/[0], month => $/[1]; + $end-bar ~~ / ^ ( \d ** 2 ) '-' ( \d ** 2 ) $ /; + my $bar-days-end = DateTime.new: year => 2022, day => $/[0], month => $/[1]; + + my $days = 0; + while ( $foo-days-start < $foo-days-end ) { + $days++ if ( $bar-days-end >= $foo-days-start >= $bar-days-start ); + $foo-days-start .= later( days => 1 ); + } + + say $days; +} -- cgit From 683a336d675d35a38cfb496d21a449977d9fcaef Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 17 Oct 2022 09:54:11 +0200 Subject: Task 2 done --- challenge-187/luca-ferrari/raku/ch-2.p6 | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 challenge-187/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-187/luca-ferrari/raku/ch-2.p6 b/challenge-187/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..9253317f2d --- /dev/null +++ b/challenge-187/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,30 @@ +#!raku + +# Perl Weekly Challenge 187 + +sub MAIN( *@n where { @n.grep( * ~~ Int ).elems == @n.elems } ) { + + my $max = 0; + my @triplet; + for 0 ..^ @n.elems -> $index-a { + my $a = @n[ $index-a ].Int; + + for $index-a ^..^ @n.elems -> $index-b { + my $b = @n[ $index-b ].Int; + + for $index-b ^..^ @n.elems -> $index-c { + my $c = @n[ $index-c ].Int; + + if ( $max <= $a + $b + $c + && ( $a + $b ) > $c + && ( $b + $c ) > $a + && ( $a + $c ) > $b ) { + @triplet = $a, $b, $c; + $max = $a + $b + $c; + } + } + } + } + + @triplet.sort.say if ( @triplet ); +} -- cgit From e1b6e75c2e06431f61f1537af5a1b387b6bd72f9 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 17 Oct 2022 11:28:12 +0200 Subject: Fix task 1 --- challenge-187/luca-ferrari/raku/ch-1.p6 | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/challenge-187/luca-ferrari/raku/ch-1.p6 b/challenge-187/luca-ferrari/raku/ch-1.p6 index 4f795ef3c8..792c650429 100644 --- a/challenge-187/luca-ferrari/raku/ch-1.p6 +++ b/challenge-187/luca-ferrari/raku/ch-1.p6 @@ -15,9 +15,15 @@ sub MAIN( Str $start-foo, Str $end-foo, Str $start-bar, Str $end-bar ) { my $bar-days-end = DateTime.new: year => 2022, day => $/[0], month => $/[1]; my $days = 0; - while ( $foo-days-start < $foo-days-end ) { - $days++ if ( $bar-days-end >= $foo-days-start >= $bar-days-start ); - $foo-days-start .= later( days => 1 ); + my $start = $foo-days-start >= $bar-days-start ?? $bar-days-start !! $foo-days-start; + my $end = $foo-days-end >= $bar-days-end ?? $foo-days-end !! $bar-days-end; + + while ( $start < $end ) { + $days++ if ( $start >= $foo-days-start + && $start <= $foo-days-end + && $start >= $bar-days-start + && $start <= $bar-days-end ); + $start .= later( days => 1 ); } say $days; -- cgit From f50a1e5e7dd6a30f1a63732a76e277a57260c142 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 17 Oct 2022 12:14:24 +0200 Subject: Task 1 and 2 PLPerl --- challenge-187/luca-ferrari/postgresql/ch-1.plperl | 45 ++++++++++++++++++++ challenge-187/luca-ferrari/postgresql/ch-2.plperl | 51 +++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 challenge-187/luca-ferrari/postgresql/ch-1.plperl create mode 100644 challenge-187/luca-ferrari/postgresql/ch-2.plperl diff --git a/challenge-187/luca-ferrari/postgresql/ch-1.plperl b/challenge-187/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..b4f2b0f93b --- /dev/null +++ b/challenge-187/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,45 @@ +-- Perl Weekly Challenge 187 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc187; + +CREATE OR REPLACE FUNCTION +pwc187.task1_plperl( text, text, text, text ) +RETURNS INT +AS $CODE$ +use DateTime; +my ( $foo_start, $foo_end, $bar_start, $bar_end ) = @_; +my @dates; + +for ( $foo_start, $foo_end, $bar_start, $bar_end ) { + $_ =~ / ^ (\d{2}) - (\d{2}) $ /x; + push @dates, DateTime->new( year => 2022, day => $1, month => $2 ); +} + +my $days = 0; + +if ( DateTime->compare( $dates[ 0 ], $dates[ 3 ] ) > 0 + || DateTime->compare( $dates[ 1 ], $dates[ 2 ] ) < 0 ) { + # one ends before the begin of the other or viceversa + $days = 0; +} +else { + my ( $start, $end ); + $start = DateTime->compare( $dates[ 0 ], $dates[ 2 ] ) <= 0 ? $dates[ 0 ] : $dates[ 2 ]; + $end = DateTime->compare( $dates[ 1 ], $dates[ 3 ] ) <= 0 ? $dates[ 1 ] : $dates[ 3 ]; + + while ( DateTime->compare( $start, $end ) <= 0 ) { + $days++ if ( DateTime->compare( $start, $dates[ 0 ] ) >= 0 + && DateTime->compare( $start, $dates[ 1 ] ) <= 0 + && DateTime->compare( $start, $dates[ 2 ] ) >= 0 + && DateTime->compare( $start, $dates[ 3 ] ) <= 0 ); + $start = $start->add( days => 1 ); + } +} + + + +return $days; + +$CODE$ +LANGUAGE plperlu; diff --git a/challenge-187/luca-ferrari/postgresql/ch-2.plperl b/challenge-187/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..cb642d4673 --- /dev/null +++ b/challenge-187/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,51 @@ +-- Perl Weekly Challenge 187 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc187; + +CREATE OR REPLACE FUNCTION +pwc187.task2_plperl() +RETURNS VOID +AS $CODE$ +$CODE$ +LANGUAGE plperl; +-- Perl Weekly Challenge 187 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc187; + +CREATE OR REPLACE FUNCTION +pwc187.task2_plperl( int[] ) +RETURNS int[] +AS $CODE$ +my ( @n ) = $_[0]->@*; +my @triplets; +my $max = 0; + + +for my $index_a ( 0 .. $#n ) { + my $a = $n[ $index_a ]; + + for my $index_b ( ( $index_a + 1 ) .. $#n ) { + my $b = $n[ $index_b ]; + + for my $index_c ( ( $index_b + 1 ) .. $#n ) { + my $c = $n[ $index_c ]; + + if ( $max <= $a + $b + $c + && ( $a + $b ) > $c + && ( $b + $c ) > $a + && ( $a + $c ) > $b ) { + + @triplets = ( $a, $b, $c ); + $max = $a + $b + $c; + } + } + } +} + + +return [@triplets]; + +$CODE$ +LANGUAGE plperl; -- cgit From 1ec78ddc196cd94ca8f197351bd0ad15bb18a0d0 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 17 Oct 2022 12:25:37 +0200 Subject: Task 1 plpgsql --- challenge-187/luca-ferrari/postgresql/ch-1.sql | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 challenge-187/luca-ferrari/postgresql/ch-1.sql diff --git a/challenge-187/luca-ferrari/postgresql/ch-1.sql b/challenge-187/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..7398ebc930 --- /dev/null +++ b/challenge-187/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,43 @@ +-- Perl Weekly Challenge 187 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc187; + +CREATE OR REPLACE FUNCTION +pwc187.task1_plpgsql( foo_start date, foo_end date, bar_start date, bar_end date ) +RETURNS int +AS $CODE$ +DECLARE + day_start date; + day_end date; + day_count int := 0; +BEGIN + + IF NOT (foo_start, foo_end) OVERLAPS (bar_start, bar_end) THEN + RETURN 0; + END IF; + + IF foo_start > bar_start THEN + day_start := bar_start; + ELSE + day_start := foo_start; + END IF; + + IF foo_end > bar_start THEN + day_end := foo_end; + ELSE + day_end := bar_end; + END IF; + + WHILE day_start <= day_end LOOP + IF day_start >= foo_start AND day_start <= foo_end AND day_start >= bar_start AND day_end <= bar_end THEN + day_count := day_count + 1 ; + END IF; + day_start := day_start + 1; + END LOOP; + + RETURN day_count; + +END +$CODE$ +LANGUAGE plpgsql; -- cgit From a0f25c254c35d5a27690703cccd9d6acd0131ce8 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 17 Oct 2022 12:30:10 +0200 Subject: Task 2 plpgsql --- challenge-187/luca-ferrari/postgresql/ch-2.sql | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 challenge-187/luca-ferrari/postgresql/ch-2.sql diff --git a/challenge-187/luca-ferrari/postgresql/ch-2.sql b/challenge-187/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..f9659e9ac2 --- /dev/null +++ b/challenge-187/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,15 @@ +-- Perl Weekly Challenge 187 +-- Task 2 + + +CREATE SCHEMA IF NOT EXISTS pwc187; + +CREATE OR REPLACE FUNCTION +pwc187.task2_plpgsql( a int[] ) +RETURNS int[] +AS $CODE$ +BEGIN + RETURN pwc187.task2_plperl( a ); +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 3a90b6c674db1f362ffc3ce64c5b2618d118383b Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 17 Oct 2022 13:46:34 +0200 Subject: Blog references --- challenge-187/luca-ferrari/blog-1.txt | 1 + challenge-187/luca-ferrari/blog-2.txt | 1 + challenge-187/luca-ferrari/blog-3.txt | 1 + challenge-187/luca-ferrari/blog-4.txt | 1 + challenge-187/luca-ferrari/blog-5.txt | 1 + challenge-187/luca-ferrari/blog-6.txt | 1 + 6 files changed, 6 insertions(+) create mode 100644 challenge-187/luca-ferrari/blog-1.txt create mode 100644 challenge-187/luca-ferrari/blog-2.txt create mode 100644 challenge-187/luca-ferrari/blog-3.txt create mode 100644 challenge-187/luca-ferrari/blog-4.txt create mode 100644 challenge-187/luca-ferrari/blog-5.txt create mode 100644 challenge-187/luca-ferrari/blog-6.txt diff --git a/challenge-187/luca-ferrari/blog-1.txt b/challenge-187/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..17ddd2e421 --- /dev/null +++ b/challenge-187/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/10/17/PerlWeeklyChallenge187.html#task1 diff --git a/challenge-187/luca-ferrari/blog-2.txt b/challenge-187/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..5af7d87940 --- /dev/null +++ b/challenge-187/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/10/17/PerlWeeklyChallenge187.html#task2 diff --git a/challenge-187/luca-ferrari/blog-3.txt b/challenge-187/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..0ac70011c4 --- /dev/null +++ b/challenge-187/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/10/17/PerlWeeklyChallenge187.html#task1plperl diff --git a/challenge-187/luca-ferrari/blog-4.txt b/challenge-187/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..ff3239f6d5 --- /dev/null +++ b/challenge-187/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/10/17/PerlWeeklyChallenge187.html#task2plperl diff --git a/challenge-187/luca-ferrari/blog-5.txt b/challenge-187/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..06274847f6 --- /dev/null +++ b/challenge-187/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/10/17/PerlWeeklyChallenge187.html#task1plpgsql diff --git a/challenge-187/luca-ferrari/blog-6.txt b/challenge-187/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..f073f961bf --- /dev/null +++ b/challenge-187/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/10/17/PerlWeeklyChallenge187.html#task2plpgsql -- cgit