aboutsummaryrefslogtreecommitdiff
path: root/challenge-128/james-smith
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-08-30 11:16:23 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-08-30 11:16:23 +0100
commitfeb3af2ea751d32d7d7e97c60e86866c5840fa5e (patch)
tree491f4fea9b24aecbde4c634f1effd581b28387b3 /challenge-128/james-smith
parentdf88352a4c5554a840c29d0115d913816cf1f25c (diff)
downloadperlweeklychallenge-club-feb3af2ea751d32d7d7e97c60e86866c5840fa5e.tar.gz
perlweeklychallenge-club-feb3af2ea751d32d7d7e97c60e86866c5840fa5e.tar.bz2
perlweeklychallenge-club-feb3af2ea751d32d7d7e97c60e86866c5840fa5e.zip
some minor tidyin up
Diffstat (limited to 'challenge-128/james-smith')
-rw-r--r--challenge-128/james-smith/perl/ch-1.pl16
-rw-r--r--challenge-128/james-smith/perl/ch-2.pl6
2 files changed, 9 insertions, 13 deletions
diff --git a/challenge-128/james-smith/perl/ch-1.pl b/challenge-128/james-smith/perl/ch-1.pl
index ab507a7726..bd121f5026 100644
--- a/challenge-128/james-smith/perl/ch-1.pl
+++ b/challenge-128/james-smith/perl/ch-1.pl
@@ -25,10 +25,8 @@ is( "@{ find_empty( $_->[0]) }", $_->[1] ) foreach @TESTS;
done_testing();
sub find_empty {
- my @rows = @{$_[0]};
- my $h = @rows-1;
- my $w = @{$rows[0]}-1;
- my @runs = map { [1 - $_->[-1]] } @rows;
+ my @runs = map { [1 - $_->[-1]] } my @rows = @{$_[0]};
+ my ($h,$w) = ( @rows-1, @{$rows[0]}-1 );
## First pass through the array - we calculate how many
## 0s are in the cell and to the right... So for example 1 we get
@@ -37,9 +35,7 @@ sub find_empty {
## 0 5 4 3 2 1
## This is O(n^2)
foreach my $i (reverse 0..$w-1) {
- foreach my $j (0..$h) {
- unshift @{$runs[$j]}, $rows[$j][$i] ? 0 : $runs[$j][0]+1;
- }
+ unshift @{$runs[$_]}, $rows[$_][$i] ? 0 : $runs[$_][0]+1 foreach 0..$h;
}
## Now we have to loop over all squares and check rectangles starting
## at the square and going down and to the right...
@@ -54,9 +50,9 @@ sub find_empty {
next unless $runs[$y][$x]; ## Short cut answer will be 0
my $max_w = 1e9;
foreach my $j ( $y..$h ) {
- $max_w = $runs[$j][$x] if $runs[$j][$x] < $max_w;
- last unless $max_w; ## Short cut all subsequent answers will zero
- my $area = $max_w * ($j-$y+1);
+ last unless $runs[$j][$x]; ## Short cut all subsequent answers will zero
+ $max_w = $runs[$j][$x] if $runs[$j][$x] < $max_w;
+ my $area = $max_w * ($j-$y+1);
$max_area = [$area,$max_w,$j-$y+1] if $area>$max_area->[0];
}
}
diff --git a/challenge-128/james-smith/perl/ch-2.pl b/challenge-128/james-smith/perl/ch-2.pl
index 05b63d47ea..0ddd7d92ff 100644
--- a/challenge-128/james-smith/perl/ch-2.pl
+++ b/challenge-128/james-smith/perl/ch-2.pl
@@ -17,7 +17,7 @@ my @TESTS = (
3 ]
);
-is( bump_platform( $_->[0], $_->[1] ), $_->[2] ) foreach @TESTS;
+is( bump_platform( $_->[0], $_->[1] ), $_->[2] ) foreach @TESTS;
is( bump_platform_keep_trains( $_->[0], $_->[1] ), $_->[2] ) foreach @TESTS;
done_testing();
@@ -42,7 +42,7 @@ sub bump_platform_keep_trains {
my @arr = @{shift @_};
my @dep = @{shift @_};
my $t = 0;
- my @plat; # = ( [ [shift @{$arr}, shift @{$dep}, my $t=1] ] );
+ my @plat;
OUTER: foreach my $st (@arr) {
foreach(@plat) {
next unless $st gt $_->[-1][1];
@@ -51,7 +51,7 @@ sub bump_platform_keep_trains {
}
push @plat, [ [ $st, (shift @dep), ++$t ] ];
}
- print Dumper( \@plat );
+ say ' ',join ' ', map { "Train $_->[2]: $_->[0]-$_->[1]" } @{$_} foreach @plat;
return scalar @plat;
}