#!/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.")