From 8a3c6cf93015da059f25c462cd2d0dc98c35305b Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Wed, 8 May 2024 19:46:55 -0700 Subject: Updates to my solutions to PWCC 268. --- challenge-268/robbie-hatley/perl/ch-1.pl | 15 ++++++++------- challenge-268/robbie-hatley/perl/ch-2.pl | 16 ++++++++-------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/challenge-268/robbie-hatley/perl/ch-1.pl b/challenge-268/robbie-hatley/perl/ch-1.pl index f597ebc58b..82efc9d48b 100755 --- a/challenge-268/robbie-hatley/perl/ch-1.pl +++ b/challenge-268/robbie-hatley/perl/ch-1.pl @@ -44,13 +44,13 @@ PROBLEM NOTES: I'll sort both arrays then subtract the second from the first. If all elements of the difference are the same, that common value is our "magic number", otherwise return "none": -sub magic ($matref) { - my @row1 = sort {$a<=>$b} @{$$matref[0]}; - my @row2 = sort {$a<=>$b} @{$$matref[1]}; - my @diff = map {$$_[1]-$$_[0]} zip6 @row1, @row2; - all {$diff[0] == $_} @diff and return $diff[0] - or return 'none'; -} + sub magic ($matref) { + my @row1 = sort {$a<=>$b} @{$$matref[0]}; + my @row2 = sort {$a<=>$b} @{$$matref[1]}; + my @diff = map {$$_[1]-$$_[0]} zip6 @row1, @row2; + all {$diff[0] == $_} @diff and return $diff[0] + or return 'none'; + } -------------------------------------------------------------------------------------------------------------- IO NOTES: @@ -82,6 +82,7 @@ sub is_possaon ($matref) { return 1; } +# Determine "magic number" (if any) for given matrix: sub magic ($matref) { my @row1 = sort {$a<=>$b} @{$$matref[0]}; my @row2 = sort {$a<=>$b} @{$$matref[1]}; diff --git a/challenge-268/robbie-hatley/perl/ch-2.pl b/challenge-268/robbie-hatley/perl/ch-2.pl index 4bb77dc174..a48e66a289 100755 --- a/challenge-268/robbie-hatley/perl/ch-2.pl +++ b/challenge-268/robbie-hatley/perl/ch-2.pl @@ -37,15 +37,15 @@ PROBLEM NOTES: This is equivalent to first sorting each array in increasing numeric order ("sort {$a<=>$b} @array"), then swapping pairs. Something like this: -sub stairway (@array) { - my @zigzag = sort {$a<=>$b} @array; - for ( my $i = 0 ; $i <= $#zigzag - 1 ; $i += 2 ) { - my $temp = $zigzag[$i]; - $zigzag[$i] = $zigzag[$i+1]; - $zigzag[$i+1] = $temp; + sub stairway (@array) { + my @zigzag = sort {$a<=>$b} @array; + for ( my $i = 0 ; $i <= $#zigzag - 1 ; $i += 2 ) { + my $temp = $zigzag[$i]; + $zigzag[$i] = $zigzag[$i+1]; + $zigzag[$i+1] = $temp; + } + return @zigzag; } - return @zigzag; -} -------------------------------------------------------------------------------------------------------------- IO NOTES: -- cgit