aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-08-30 11:25:35 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-08-30 11:25:35 +0100
commit154cd35be8cff200829935f4278c9ff289be3362 (patch)
treee33fe9fc97e16b533da92883268165fbc2430732
parent16cc035e448db0ac077c46e1b9a2a291cf660225 (diff)
downloadperlweeklychallenge-club-154cd35be8cff200829935f4278c9ff289be3362.tar.gz
perlweeklychallenge-club-154cd35be8cff200829935f4278c9ff289be3362.tar.bz2
perlweeklychallenge-club-154cd35be8cff200829935f4278c9ff289be3362.zip
updated ch-2.pl with comments and reduced code length
-rw-r--r--challenge-128/james-smith/perl/ch-2.pl42
1 files changed, 26 insertions, 16 deletions
diff --git a/challenge-128/james-smith/perl/ch-2.pl b/challenge-128/james-smith/perl/ch-2.pl
index 0ddd7d92ff..bbda141d19 100644
--- a/challenge-128/james-smith/perl/ch-2.pl
+++ b/challenge-128/james-smith/perl/ch-2.pl
@@ -26,32 +26,42 @@ sub bump_platform {
my @arr = @{shift @_};
my @dep = @{shift @_};
#shift @{$arr};
- my @plat = ();#(shift @{$dep});
+ my @platforms = ();#(shift @{$dep});
OUTER: foreach my $st (@arr) {
- foreach(0..$#plat) {
- next unless $st gt $plat[$_];
- $plat[$_] = shift @dep;
- next OUTER;
+ foreach(0..$#platforms) {
+ ## If train fits on platform - we extend the last departure time
+ ## and start working with new train
+ ($platforms[$_] = shift @dep) && (next OUTER) if $st gt $platforms[$_];
}
- push @plat,shift @dep;
+ ## Otherwise we start a new platform...
+ push @platforms,shift @dep;
}
- return scalar @plat;
+ return scalar @platforms;
}
sub bump_platform_keep_trains {
my @arr = @{shift @_};
my @dep = @{shift @_};
- my $t = 0;
- my @plat;
+ my($train_no, @platforms) = (0);
+
OUTER: foreach my $st (@arr) {
- foreach(@plat) {
- next unless $st gt $_->[-1][1];
- push @{$_}, [ $st, (shift @dep), ++$t ];
- next OUTER;
+ foreach(@platforms) {
+ ## If we can fit on this platform - add train details
+ ## and go to the next train
+ (push @{$_}, [ $st, (shift @dep), ++$train_no ]) &&
+ (next OUTER) if $st gt $_->[-1][1];
}
- push @plat, [ [ $st, (shift @dep), ++$t ] ];
+ ## No room on any of the existing platforms - so we create
+ ## a new one and push the train
+ push @platforms, [ [ $st, (shift @dep), ++$train_no ] ];
}
- say ' ',join ' ', map { "Train $_->[2]: $_->[0]-$_->[1]" } @{$_} foreach @plat;
- return scalar @plat;
+ ## Display details about trains on platform.
+ say ' ',
+ join ' ',
+ map { "Train $_->[2]: $_->[0]-$_->[1]" }
+ @{$_} foreach @platforms;
+ ## Return the number of platforms
+ return scalar @platforms;
}
+