Simplify interface for testing

This commit is contained in:
2026-05-19 17:29:36 -04:00
parent 9f445b666d
commit d782d5e662
3 changed files with 35 additions and 34 deletions

View File

@@ -8,7 +8,6 @@ A report (one line of space-separated integers) is safe if:
Usage:
python day2/aoc_2024_day2.py input.txt
cat input.txt | python day2/aoc_2024_day2.py
"""
import argparse
@@ -59,13 +58,14 @@ class Report:
return True
def parse_reports(lines):
def parse_reports(path):
reports = []
for raw in lines:
line = raw.strip()
if not line:
continue
reports.append(Report.from_line(line))
with open(path, "r", encoding="utf-8") as f:
for raw in f:
line = raw.strip()
if not line:
continue
reports.append(Report.from_line(line))
return reports
@@ -77,17 +77,25 @@ def count_safe(reports):
return safe_count
def main():
parser = argparse.ArgumentParser("CLI to read sample files.")
parser.add_argument("input", help="Input file path (defaults to stdin). Use '-' for stdin.")
args = parser.parse_args()
reports = parse_reports(args.input)
def count_safe_in_file(path):
return count_safe(parse_reports(path))
print(count_safe(reports))
def main(argv=None):
parser = argparse.ArgumentParser("CLI to read sample files.")
parser.add_argument(
"input",
help="Input file path.",
)
args = parser.parse_args(argv)
return count_safe_in_file(args.input)
def cli(argv=None):
print(main(argv))
return 0
if __name__ == "__main__":
main()
raise SystemExit(cli())