aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2021-08-30 10:53:37 +0200
committerLuca Ferrari <fluca1978@gmail.com>2021-08-30 10:53:37 +0200
commit252ac2f826a6cc3bf20ff55303901330809996e5 (patch)
tree5f18a5317c0bd39b01d32187da6c07c322afd50d
parentefe18c028ec89274497049eb1ed8a76d37863483 (diff)
downloadperlweeklychallenge-club-252ac2f826a6cc3bf20ff55303901330809996e5.tar.gz
perlweeklychallenge-club-252ac2f826a6cc3bf20ff55303901330809996e5.tar.bz2
perlweeklychallenge-club-252ac2f826a6cc3bf20ff55303901330809996e5.zip
Task 2 done
-rw-r--r--challenge-128/luca-ferrari/raku/ch-2.p644
1 files changed, 44 insertions, 0 deletions
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;
+
+
+}