From 0bece7cc77cacd86203f5cef6b995f4e67f7f8bb Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Thu, 17 Jun 2021 19:51:04 +0000 Subject: Small optimizations and clean up --- challenge-117/perlboy1967/perl/ch-1.pl | 10 +++++++--- challenge-117/perlboy1967/perl/ch-2.pl | 10 +++++----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/challenge-117/perlboy1967/perl/ch-1.pl b/challenge-117/perlboy1967/perl/ch-1.pl index 0252ba108a..b268b229c6 100755 --- a/challenge-117/perlboy1967/perl/ch-1.pl +++ b/challenge-117/perlboy1967/perl/ch-1.pl @@ -11,14 +11,18 @@ use v5.16; use strict; use warnings; -use File::Slurp; +use File::Basename qw(dirname); +use File::Slurp qw(read_file); use List::MoreUtils qw(slide); -printf "Missing row number(s) of file '%s' is/are '%s'\n", $ARGV[0], join(',',missingRows($ARGV[0])); +my $input = $ARGV[0] // sprintf('%s/input.txt',dirname($0)); + +printf "Missing row number(s) of file '%s' is/are '%s'\n", + $input, join(',',missingRows($input)); sub missingRows { my ($f) = @_; - return grep /\d/,slide{($a+1..$b-1)if($b-$a>1)}sort{$a<=>$b}map{chomp;s/^(\d+).*/$1/;$_}read_file($f); + return grep /\d/,slide{($a+1..$b-1)if($b-$a>1)}sort{$a<=>$b}map{/^(\d+).*/;$_=$1}read_file($f); } diff --git a/challenge-117/perlboy1967/perl/ch-2.pl b/challenge-117/perlboy1967/perl/ch-2.pl index 19a3b62f61..a6b0a6f00f 100755 --- a/challenge-117/perlboy1967/perl/ch-2.pl +++ b/challenge-117/perlboy1967/perl/ch-2.pl @@ -11,11 +11,8 @@ use v5.16; use strict; use warnings; -use List::Util qw(sum); - use Test::More; use Test::Deep; -use Data::Printer; # Prototype(s) sub findPossiblePaths($); @@ -29,14 +26,17 @@ my %tests = ( ); foreach my $test (sort keys %tests) { - cmp_deeply (findPossiblePaths($test),$tests{$test},"findPossiblePaths($test)"); + cmp_deeply(findPossiblePaths($test), + $tests{$test}, + "findPossiblePaths($test)"); } done_testing; print "Now some bigger challenges...\n"; for my $size (4 .. 10) { - printf "findPossiblePaths($size) = %d paths\n", scalar @{findPossiblePaths($size)}; + printf "findPossiblePaths($size) = %d paths\n", + scalar @{findPossiblePaths($size)}; } -- cgit From 0f3f1e13ceb7a3942cfcc6174faf9d582e90b830 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Thu, 17 Jun 2021 19:55:52 +0000 Subject: ... remove clutter from regexp ... --- challenge-117/perlboy1967/perl/ch-1.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-117/perlboy1967/perl/ch-1.pl b/challenge-117/perlboy1967/perl/ch-1.pl index b268b229c6..1a22567af0 100755 --- a/challenge-117/perlboy1967/perl/ch-1.pl +++ b/challenge-117/perlboy1967/perl/ch-1.pl @@ -24,5 +24,5 @@ printf "Missing row number(s) of file '%s' is/are '%s'\n", sub missingRows { my ($f) = @_; - return grep /\d/,slide{($a+1..$b-1)if($b-$a>1)}sort{$a<=>$b}map{/^(\d+).*/;$_=$1}read_file($f); + return grep /\d/,slide{($a+1..$b-1)if($b-$a>1)}sort{$a<=>$b}map{/^(\d+)/;$_=$1}read_file($f); } -- cgit From 334b8f46cfbe5f0f6687c443cc8ce86ca0cc0c30 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Thu, 17 Jun 2021 20:02:45 +0000 Subject: Ditch File::Slurp. No need for this time. --- challenge-117/perlboy1967/perl/ch-1.pl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/challenge-117/perlboy1967/perl/ch-1.pl b/challenge-117/perlboy1967/perl/ch-1.pl index 1a22567af0..109b8e42e7 100755 --- a/challenge-117/perlboy1967/perl/ch-1.pl +++ b/challenge-117/perlboy1967/perl/ch-1.pl @@ -12,7 +12,6 @@ use strict; use warnings; use File::Basename qw(dirname); -use File::Slurp qw(read_file); use List::MoreUtils qw(slide); my $input = $ARGV[0] // sprintf('%s/input.txt',dirname($0)); @@ -23,6 +22,8 @@ printf "Missing row number(s) of file '%s' is/are '%s'\n", sub missingRows { my ($f) = @_; + + open(my $fh,'<',$f) || die; - return grep /\d/,slide{($a+1..$b-1)if($b-$a>1)}sort{$a<=>$b}map{/^(\d+)/;$_=$1}read_file($f); + return grep /\d/,slide{($a+1..$b-1)if($b-$a>1)}sort{$a<=>$b}map{/^(\d+)/;$_=$1}<$fh>; } -- cgit