diff options
| -rw-r--r-- | challenge-333/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-333/dave-jacoby/perl/ch-1.pl | 46 | ||||
| -rw-r--r-- | challenge-333/dave-jacoby/perl/ch-2.pl | 37 |
3 files changed, 84 insertions, 0 deletions
diff --git a/challenge-333/dave-jacoby/blog.txt b/challenge-333/dave-jacoby/blog.txt new file mode 100644 index 0000000000..ed73ae2fe0 --- /dev/null +++ b/challenge-333/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby-lpwk.onrender.com/2025/08/07/weekly-challenge-333-back-in-the-saddle-again.html 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] ); +} diff --git a/challenge-333/dave-jacoby/perl/ch-2.pl b/challenge-333/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..74b88695d9 --- /dev/null +++ b/challenge-333/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +my @examples = ( + + [ 1, 0, 2, 3, 0, 4, 5, 0 ], + [ 1, 2, 3 ], + [ 1, 2, 3, 0 ], + [ 0, 0, 1, 2 ], + [ 1, 2, 0, 3, 4 ], + +); + +for my $input (@examples) { + my $str = join ',', $input->@*; + my @output = duplicate_zeroes( $input->@* ); + my $output = join ',', @output; + say <<"END"; + Input: \@str = ($str) + Output: ($output) +END +} + +sub duplicate_zeroes (@ints) { + my @output; + my $l = $#ints; + for my $i ( 0 .. $l ) { + my $n = $ints[$i]; + my $o = $#output; + push @output, $n if $#output < $l; + push @output, $n if $n == 0 && $#output < $l; + } + return @output; +} |
