From efe18c028ec89274497049eb1ed8a76d37863483 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 30 Aug 2021 10:34:26 +0200 Subject: Task 1 done --- challenge-128/luca-ferrari/raku/ch-1.p6 | 59 +++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 challenge-128/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-128/luca-ferrari/raku/ch-1.p6 b/challenge-128/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..180ce1ba7a --- /dev/null +++ b/challenge-128/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,59 @@ +#!raku + +sub MAIN() { + my @matrix = [ 1 ,0, 0, 0, 1, 0 ], + [ 1 ,1, 0, 0, 0, 1 ], + [ 1, 0, 0, 0, 0, 0 ]; + + my @zeroes; + + for 0 ..^ @matrix.elems -> $current-row { + for 0 ..^ @matrix[ $current-row ].elems -> $current-column { + my $zeroes-count = 0; + my $column = $current-column; + my $previous-column = $current-column; + while ( $column < @matrix[ $current-row ].elems && $column - $previous-column <= 1 ) { + $zeroes-count++ if @matrix[ $current-row ][ $column ] == 0; + last if @matrix[ $current-row ][ $column ] != 0; + $previous-column = $column++; + } + + @zeroes[ $current-row ][ $current-column ] = $zeroes-count; + } + } + + say @zeroes; + + my $rows = 0; + my $cols = 0; + my @sub-matrix; + my $max = 0; + for 0 ..^ @zeroes.elems -> $current-row { + for 0 ..^ @zeroes[ $current-row ].elems -> $current-column { + next if @zeroes[ $current-row ][ $current-column ] == 0; + $cols = @zeroes[ $current-row ][ $current-column ]; + $rows = 1; + + + for $current-row + 1 ..^ @zeroes.elems -> $next-row { + $rows = 0 and last if @zeroes[ $next-row ][ $current-column ] == 0; + $rows++ if @zeroes[ $next-row ][ $current-column ] != 0; + $cols = min( $cols, @zeroes[ $next-row ][ $current-column ] ); + } + + + $max = $rows * $cols if $rows * $cols > $max; + @sub-matrix.push: [ $rows * $cols, $current-row, $current-column, $current-row + $rows - 1, $current-column + $cols - 1 ] if $rows * $cols > 0 && $rows * $cols >= $max; + + + + + } + } + + "{ $_[ 0 ] } zeroes starting from <{ $_[ 1 ] }, { $_[ 2 ]}> to <{ $_[ 3 ] }, { $_[ 4 ]}>".say for @sub-matrix; + + + + +} -- cgit From 252ac2f826a6cc3bf20ff55303901330809996e5 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 30 Aug 2021 10:53:37 +0200 Subject: Task 2 done --- challenge-128/luca-ferrari/raku/ch-2.p6 | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 challenge-128/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-128/luca-ferrari/raku/ch-2.p6 b/challenge-128/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..8fb549c3ba --- /dev/null +++ b/challenge-128/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,44 @@ +#!raku + + +class Train { + has Int $.hour-arrival; + has Int $.hour-departure; + has Int $.minute-arrival; + has Int $.minute-departure; + + + submethod BUILD( Str :$arrival, Str :$departure ) { + $arrival ~~ / (\d+) ':' (\d+) /; + ( $!hour-arrival, $!minute-arrival ) = $/[ 0 ].Int, $/[ 1 ].Int; + $departure ~~ / (\d+) ':' (\d+) / ; + ( $!hour-departure, $!minute-departure ) = $/[ 0 ].Int, $/[ 1 ].Int; + } + + method collide( Train $other-train ) { + my $this-arrival = $!hour-arrival * 60 + $!minute-arrival; + my $this-departure = $!hour-departure * 60 + $!minute-departure; + my $other-train-arrival = $other-train.hour-arrival * 60 + $other-train.minute-arrival; + my $other-train-departure = $other-train.hour-departure * 60 + $other-train.minute-departure; + + $this-arrival <= $other-train-arrival && $other-train-departure <= $this-departure; + } +} + + +sub MAIN() { + my @arrivals = '10:20', '11:00', '11:10', '12:20', '16:20', '19:00'; + my @departures = '10:30', '13:20', '12:40', '12:50', '20:20', '21:20'; + + + my @trains.push: Train.new: arrival => @arrivals[ $_ ], departure => @departures[ $_ ] for 0 ..^ @arrivals.elems; + + my $collisions = 0; + for 0 ..^ @trains.elems -> $current-train { + $collisions++ if @trains[ $current-train ].collide( @trains[ $_ ] ) for $current-train + 1 ..^ @trains.elems; + } + + "Required platforms: { $collisions + 1 }".say; + + +} -- cgit From ae9d4bc0229c54030167383b807cf48e87d82bef Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 30 Aug 2021 11:15:19 +0200 Subject: Reset the array when a new wider matrix is found --- challenge-128/luca-ferrari/raku/ch-1.p6 | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/challenge-128/luca-ferrari/raku/ch-1.p6 b/challenge-128/luca-ferrari/raku/ch-1.p6 index 180ce1ba7a..9294247d68 100644 --- a/challenge-128/luca-ferrari/raku/ch-1.p6 +++ b/challenge-128/luca-ferrari/raku/ch-1.p6 @@ -22,8 +22,7 @@ sub MAIN() { } } - say @zeroes; - + my $rows = 0; my $cols = 0; my @sub-matrix; @@ -42,7 +41,7 @@ sub MAIN() { } - $max = $rows * $cols if $rows * $cols > $max; + $max = $rows * $cols and @sub-matrix = () if $rows * $cols > $max; @sub-matrix.push: [ $rows * $cols, $current-row, $current-column, $current-row + $rows - 1, $current-column + $cols - 1 ] if $rows * $cols > 0 && $rows * $cols >= $max; -- cgit From f6d2eb07c262a9d95eadb566399a090eed9c6d83 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 30 Aug 2021 11:20:12 +0200 Subject: blog references --- challenge-128/luca-ferrari/blog-1.txt | 1 + challenge-128/luca-ferrari/blog-2.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 challenge-128/luca-ferrari/blog-1.txt create mode 100644 challenge-128/luca-ferrari/blog-2.txt diff --git a/challenge-128/luca-ferrari/blog-1.txt b/challenge-128/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..e224f47754 --- /dev/null +++ b/challenge-128/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/08/30/PerlWeeklyChallenge128.html#task1 diff --git a/challenge-128/luca-ferrari/blog-2.txt b/challenge-128/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..443735006c --- /dev/null +++ b/challenge-128/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/08/30/PerlWeeklyChallenge128.html#task2 -- cgit