aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-117/mohammad-anwar/perl/ch-1.pl11
1 files changed, 8 insertions, 3 deletions
diff --git a/challenge-117/mohammad-anwar/perl/ch-1.pl b/challenge-117/mohammad-anwar/perl/ch-1.pl
index c9aebacadd..15d78ca33a 100644
--- a/challenge-117/mohammad-anwar/perl/ch-1.pl
+++ b/challenge-117/mohammad-anwar/perl/ch-1.pl
@@ -7,7 +7,8 @@ my $F = $ARGV[0];
die "ERROR: Missing input file.\n" unless defined $F;
die "ERROR: Invalid file $F.\n" unless -f $F;
-my $line_sum = 0;
+my $line_sum = 0;
+my $row_count = 0;
open (my $fh, '<:encoding(utf8)', $F)
or die "ERROR: Unable to open file $F: $!\n";
@@ -15,11 +16,15 @@ while (my $row = <$fh>) {
chomp $row;
my ($line_number) = split /,/,$row;
$line_sum += $line_number;
+ $row_count++;
}
close($fh);
-# Sum of 1..15
-my $expected = (15 * 16)/2;
+# Assuming one missing row in the file.
+$row_count++;
+
+# Now find the sum of line numbers.
+my $expected = ($row_count * ($row_count + 1))/2;
print sprintf("Missing line: %d\n", $expected - $line_sum);