diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2025-08-07 18:26:15 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2025-08-07 18:26:15 -0400 |
| commit | 0319aca6f1cea4b160b9797dba0041a9f82fad8b (patch) | |
| tree | 8dccccdb38310e4675da704b53b20c363cc1f1d2 /challenge-333/dave-jacoby/perl/ch-1.pl | |
| parent | 7b607f3fe5f13d629216d83fb7d25626a4dc98f5 (diff) | |
| download | perlweeklychallenge-club-0319aca6f1cea4b160b9797dba0041a9f82fad8b.tar.gz perlweeklychallenge-club-0319aca6f1cea4b160b9797dba0041a9f82fad8b.tar.bz2 perlweeklychallenge-club-0319aca6f1cea4b160b9797dba0041a9f82fad8b.zip | |
DAJ 333 blogged
Diffstat (limited to 'challenge-333/dave-jacoby/perl/ch-1.pl')
| -rw-r--r-- | challenge-333/dave-jacoby/perl/ch-1.pl | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/challenge-333/dave-jacoby/perl/ch-1.pl b/challenge-333/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..5772bdb9c3 --- /dev/null +++ b/challenge-333/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +my @examples = ( + + [ [ 2, 1 ], [ 2, 3 ], [ 2, 5 ] ], + [ [ 1, 4 ], [ 3, 4 ], [ 10, 4 ] ], + [ [ 0, 0 ], [ 1, 1 ], [ 2, 3 ] ], + [ [ 1, 1 ], [ 1, 1 ], [ 1, 1 ] ], + [ [ 1000000, 1000000 ], [ 2000000, 2000000 ], [ 3000000, 3000000 ] ], +); + +for my $input (@examples) { + my $output = straight_line( $input->@* ); + my $str = join ',', map { qq{[$_]} } map { join ', ', $_->@* } $input->@*; + say <<"END"; + Input: \@str = ($str) + Output: $output +END +} + +sub straight_line (@array) { + my %angles; + for my $i ( 0 .. $#array ) { + my $k = $array[$i]; + for my $j ( $i + 1, $#array ) { + next unless $i != $j; + next unless defined $array[$j]; + my $l = $array[$j]; + my $a1 = find_angle( $k, $l ); + my $a2 = find_angle( $l, $k ); + $angles{$a1} = 1; + $angles{$a2} = 1; + } + } + return scalar keys %angles <= 2 ? 'true' : 'false'; +} + +sub find_angle ( $p1, $p2 ) { + return + # sprintf '%.06f', + atan2( $p1->[1] - $p2->[1], $p1->[0] - $p2->[0] ); +} |
