From dcd950608cceb962813522fa61d8cd2c9aeaf782 Mon Sep 17 00:00:00 2001 From: Matthias Muth Date: Wed, 1 Mar 2023 22:42:28 +0100 Subject: Move ch-*.pl into 'perl' subdirectory. --- challenge-206/matthias-muth/ch-1.pl | 45 --------------------- challenge-206/matthias-muth/ch-2.pl | 68 -------------------------------- challenge-206/matthias-muth/perl/ch-1.pl | 45 +++++++++++++++++++++ challenge-206/matthias-muth/perl/ch-2.pl | 68 ++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 113 deletions(-) delete mode 100755 challenge-206/matthias-muth/ch-1.pl delete mode 100755 challenge-206/matthias-muth/ch-2.pl create mode 100755 challenge-206/matthias-muth/perl/ch-1.pl create mode 100755 challenge-206/matthias-muth/perl/ch-2.pl diff --git a/challenge-206/matthias-muth/ch-1.pl b/challenge-206/matthias-muth/ch-1.pl deleted file mode 100755 index a23923ea9f..0000000000 --- a/challenge-206/matthias-muth/ch-1.pl +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env perl - -use strict; -use warnings; -use feature 'signatures'; -no warnings 'experimental::signatures'; - -use List::Util qw( min ); - -sub time_diffs( $fixed, @others ) { - # Return all differences between one fixed timestamp and a list of others. - # Use the time difference spanning over midnight if it is shorter - # (by simply using the minimum of both). - return - map { my $d = abs( $fixed - $others[$_] ); min( $d, (24*60) - $d ); } - 0..$#others; -} - -sub shortest_time( @hhmm_times ) { - # Turn HH:MM times into number of minutes. - my @t = map { /^(\d+):(\d{2})$/; $1 * 60 + $2 } @hhmm_times; - # Return the minimum of the time differences of every element with all - # its successors. We can skip the last element, as it has no successor to - # build a difference with. - # We simplify the parameter list by just giving the whole - # slice instead of giving the first element and its successors separately. - return min( map time_diffs( @t[ $_ .. $#t ] ), 0 .. ( $#t - 1 ) ); -} - - -use Test::More; - -my @tests = ( - [ [ "00:04", "23:55", "20:00" ], 9 ], - [ [ "00:00", "23:55", "20:00" ], 5 ], - [ [ "01:01", "00:50", "00:57" ], 4 ], - [ [ "10:10", "09:30", "09:00", "09:55" ], 15 ], -); - -is shortest_time( @{$_->[0]} ), - $_->[1], - "shortest_time( @{$_->[0]} ) == " . ( $_->[1] // "undef" ) - for @tests; - -done_testing; diff --git a/challenge-206/matthias-muth/ch-2.pl b/challenge-206/matthias-muth/ch-2.pl deleted file mode 100755 index 76758886f5..0000000000 --- a/challenge-206/matthias-muth/ch-2.pl +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/env perl - -use strict; -use warnings; - -use feature 'signatures'; -no warnings 'experimental::signatures'; - -use List::Util qw( min max sum ); - -sub permute( $a_ref ) { - return undef unless defined $a_ref && ref $a_ref eq 'ARRAY'; - return () if @$a_ref == 0; - return $a_ref if @$a_ref == 1; - - my @permutations; - for my $i ( 0..$#$a_ref ) { - my @others = @$a_ref; - my $extracted = splice( @others, $i, 1, () ); - push @permutations, [ $extracted, @$_ ] - for permute( [ @others ] ); - } - return @permutations; -} - -sub sum_of_min_of_pairs( @a ) { - return undef - unless @a % 2 == 0; - return - sum( map $_ % 2 == 0 ? min( $a[$_], $a[ $_ + 1 ] ) : 0, 0 .. $#a - 1 ); -} - -sub max_of_sums( @a ) { - return undef - unless @a % 2 == 0; - return - max( map sum_of_min_of_pairs( @$_ ), permute( [ @a ] ) ); -} - - -use Test::More; - -my @tests = ( - [ [], undef ], - [ [ 11 ], undef ], - [ [ 11,12 ], 11 ], - [ [ 1,2,3,4,5,6 ], 9 ], - [ [ 1,2,3,4 ], 4 ], - [ [ 0,2,1,3 ], 2 ], -); - -is max_of_sums( @{$_->[0]} ), $_->[1], - "max_of_sums( @{$_->[0]} ) == " . ( $_->[1] // "undef" ) - for @tests; - -done_testing; - -__END__ - -use v5.10; - -# For testing: -for ( @tests ) { - say "Permutations of @{$_->[0]}:"; - my @permutations = permute( [ @{$_->[0]} ] ); - say " @{$_}" - for @permutations; -} diff --git a/challenge-206/matthias-muth/perl/ch-1.pl b/challenge-206/matthias-muth/perl/ch-1.pl new file mode 100755 index 0000000000..a23923ea9f --- /dev/null +++ b/challenge-206/matthias-muth/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'signatures'; +no warnings 'experimental::signatures'; + +use List::Util qw( min ); + +sub time_diffs( $fixed, @others ) { + # Return all differences between one fixed timestamp and a list of others. + # Use the time difference spanning over midnight if it is shorter + # (by simply using the minimum of both). + return + map { my $d = abs( $fixed - $others[$_] ); min( $d, (24*60) - $d ); } + 0..$#others; +} + +sub shortest_time( @hhmm_times ) { + # Turn HH:MM times into number of minutes. + my @t = map { /^(\d+):(\d{2})$/; $1 * 60 + $2 } @hhmm_times; + # Return the minimum of the time differences of every element with all + # its successors. We can skip the last element, as it has no successor to + # build a difference with. + # We simplify the parameter list by just giving the whole + # slice instead of giving the first element and its successors separately. + return min( map time_diffs( @t[ $_ .. $#t ] ), 0 .. ( $#t - 1 ) ); +} + + +use Test::More; + +my @tests = ( + [ [ "00:04", "23:55", "20:00" ], 9 ], + [ [ "00:00", "23:55", "20:00" ], 5 ], + [ [ "01:01", "00:50", "00:57" ], 4 ], + [ [ "10:10", "09:30", "09:00", "09:55" ], 15 ], +); + +is shortest_time( @{$_->[0]} ), + $_->[1], + "shortest_time( @{$_->[0]} ) == " . ( $_->[1] // "undef" ) + for @tests; + +done_testing; diff --git a/challenge-206/matthias-muth/perl/ch-2.pl b/challenge-206/matthias-muth/perl/ch-2.pl new file mode 100755 index 0000000000..76758886f5 --- /dev/null +++ b/challenge-206/matthias-muth/perl/ch-2.pl @@ -0,0 +1,68 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use feature 'signatures'; +no warnings 'experimental::signatures'; + +use List::Util qw( min max sum ); + +sub permute( $a_ref ) { + return undef unless defined $a_ref && ref $a_ref eq 'ARRAY'; + return () if @$a_ref == 0; + return $a_ref if @$a_ref == 1; + + my @permutations; + for my $i ( 0..$#$a_ref ) { + my @others = @$a_ref; + my $extracted = splice( @others, $i, 1, () ); + push @permutations, [ $extracted, @$_ ] + for permute( [ @others ] ); + } + return @permutations; +} + +sub sum_of_min_of_pairs( @a ) { + return undef + unless @a % 2 == 0; + return + sum( map $_ % 2 == 0 ? min( $a[$_], $a[ $_ + 1 ] ) : 0, 0 .. $#a - 1 ); +} + +sub max_of_sums( @a ) { + return undef + unless @a % 2 == 0; + return + max( map sum_of_min_of_pairs( @$_ ), permute( [ @a ] ) ); +} + + +use Test::More; + +my @tests = ( + [ [], undef ], + [ [ 11 ], undef ], + [ [ 11,12 ], 11 ], + [ [ 1,2,3,4,5,6 ], 9 ], + [ [ 1,2,3,4 ], 4 ], + [ [ 0,2,1,3 ], 2 ], +); + +is max_of_sums( @{$_->[0]} ), $_->[1], + "max_of_sums( @{$_->[0]} ) == " . ( $_->[1] // "undef" ) + for @tests; + +done_testing; + +__END__ + +use v5.10; + +# For testing: +for ( @tests ) { + say "Permutations of @{$_->[0]}:"; + my @permutations = permute( [ @{$_->[0]} ] ); + say " @{$_}" + for @permutations; +} -- cgit