#!/usr/bin/env perl
#use v5.20;
#use feature qw(signatures);
#no warnings qw(experimental::signatures);
use v5.36;
use autodie;
exit 1 if not @ARGV;
sub read_file ($fh, $chunk_size=65536) {
# Return the next chunk, including to the end of line.
read($fh, my $chunk, $chunk_size);
if (length($chunk) && substr($chunk, -1) ne "\n") {
return $chunk if eof($fh);
$chunk .= readline($fh);
}
return $chunk;
}
my $mul_pattern = 'mul\(\d{1,3},\d{1,3}\)';
my $filename = shift;
my $count = 0;
if (open(my $fh, '<', $filename)) {
while (length(my $chunk = read_file($fh))) {
$count += () = $chunk =~ m/$mul_pattern/g;
}
}
print "Found $count matches.\n";
####
#!/usr/bin/env python
import re, sys
if len(sys.argv) < 2: sys.exit(1)
def read_file (file, chunk_size=65536):
"""
Lazy function generator to read a file in chunks,
including to the end of line.
"""
while True:
chunk = file.read(chunk_size)
if not chunk:
break
if not chunk.endswith('\n'):
chunk += file.readline()
yield chunk
mul_re = re.compile(r"mul\(\d{1,3},\d{1,3}\)")
filename = sys.argv[1]
count = 0
try:
with open (filename, "r") as file:
for chunk in read_file(file):
count += len(mul_re.findall(chunk))
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)
print(f"Found {count} matches.")
##
##
Perl 0.463s
Found 200246 matches.
Python 0.250s
Found 200246 matches.