From d3be7070715a1574b23eb786d5143a3fa23ea8d8 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 26 Jul 2021 20:45:36 +0100 Subject: use reverse to flip keys and values in hash to make last line logic easier --- challenge-123/james-smith/perl/ch-2.pl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/challenge-123/james-smith/perl/ch-2.pl b/challenge-123/james-smith/perl/ch-2.pl index af4005104e..7dd9dde161 100644 --- a/challenge-123/james-smith/perl/ch-2.pl +++ b/challenge-123/james-smith/perl/ch-2.pl @@ -11,6 +11,8 @@ use Data::Dumper qw(Dumper); my @TESTS = ( [ [ [10,20],[20,20],[20,10],[10,10] ], 1 ], [ [ [12,24],[16,10],[20,12],[18,16] ], 0 ], + [ [ [-2,5],[2,-5],[5,2],[-5,-2] ], 1 ], + [ [ [0,1],[1,0],[0,-1],[-1,0] ], 1 ], ); is( is_square(@{$_->[0]}), $_->[1] ) foreach @TESTS; @@ -33,10 +35,7 @@ sub is_square { my $a = shift @pts; $D{($a->[0]-$_->[0])**2+($a->[1]-$_->[1])**2}++ foreach @pts; } - my @K = keys %D; - return 0 unless @K== 2; ## More than two distances - return 1 if $K[0]*2==$K[1]; ## First no is the diagonal - return 1 if $K[1]*2==$K[0]; ## First no is the edge.. - return 0; ## Equilat triangles.. + my %F = reverse %D; + return exists $F{2} && exists $F{4} && $F{2} == 2*$F{4} || 0; } -- cgit