From 8ea36e4fbfb532fa1c8e28c57967a4143de05357 Mon Sep 17 00:00:00 2001 From: Folkert Kevelam Date: Tue, 25 Feb 2025 21:46:22 +0100 Subject: [PATCH] Try to update to more modern perl --- AoC/2024/day_1/day_1.pl | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/AoC/2024/day_1/day_1.pl b/AoC/2024/day_1/day_1.pl index 0ad2e35..73f9b5a 100644 --- a/AoC/2024/day_1/day_1.pl +++ b/AoC/2024/day_1/day_1.pl @@ -2,19 +2,19 @@ use v5.40; sub day1_1( $filename ) { - open(my $FH, '<', $filename) or die $!; + open my $fh, '<', $filename; my @list1; my @list2; my $counter = 0; - while(<$FH>) { - my @data = split(" ", $_); + while(my $line = readline $fh) { + my @data = split " ", $line; if (@data == 2) { - push(@list1, $data[0]); - push(@list2, $data[1]); + push @list1, $data[0]; + push @list2, $data[1]; } else { - print("More than two data points found at: $counter\n"); + print "More than two data points found at: $counter\n"; return -1; } $counter++; @@ -23,30 +23,21 @@ sub day1_1( $filename ) { @list1 = sort @list1; @list2 = sort @list2; - if ($#list1 != $#list2) { + if (@list1 != @list2) { print("indices don't match\n"); return -1; } my $total = 0; - for( my $i = 0; $i<=$#list1; $i++) { + foreach my $i ( 0 .. $#list1) { $total += abs($list1[$i] - $list2[$i]); } - close($FH); + close $fh; return $total; } -sub round( $num ) { - my $base = int($num); - if($num - $base >= 0.5) { - $base++; - } - - return $base -} - sub find_count($item, $is_sorted, @list) { if($is_sorted != 1) { @list = sort @list;