From d782d5e662a6f54959401338b19813f084391a8d Mon Sep 17 00:00:00 2001 From: josh Date: Tue, 19 May 2026 17:29:36 -0400 Subject: [PATCH] Simplify interface for testing --- day2/day2_sample.py | 40 +++++++++++++++++++++++--------------- day2/test_cases/sample.txt | 13 ++++++------- day2/test_day2.py | 16 +++++---------- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/day2/day2_sample.py b/day2/day2_sample.py index 907d61a..9e290dc 100644 --- a/day2/day2_sample.py +++ b/day2/day2_sample.py @@ -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()) diff --git a/day2/test_cases/sample.txt b/day2/test_cases/sample.txt index 55678f8..2d6faf5 100644 --- a/day2/test_cases/sample.txt +++ b/day2/test_cases/sample.txt @@ -1,7 +1,6 @@ -7 6 4 2 1 -1 2 7 8 9 -9 7 6 2 1 -1 3 2 4 5 -8 6 4 4 1 -1 3 6 7 9 - +7 6 4 2 1 +1 2 7 8 9 +9 7 6 2 1 +1 3 2 4 5 +8 6 4 4 1 +1 3 6 7 9 \ No newline at end of file diff --git a/day2/test_day2.py b/day2/test_day2.py index 06d6886..48a59f1 100644 --- a/day2/test_day2.py +++ b/day2/test_day2.py @@ -1,32 +1,26 @@ import os import unittest -from day2 import aoc_2024_day2 +from day2_sample import count_safe_in_file class TestDay2(unittest.TestCase): - def _count_safe_from_file(self, path): - with open(path, "r", encoding="utf-8") as f: - reports = aoc_2024_day2.parse_reports(f) - return aoc_2024_day2.count_safe(reports) - def _case_path(self, filename): here = os.path.dirname(__file__) return os.path.join(here, "test_cases", filename) def test_sample(self): - self.assertEqual(self._count_safe_from_file(self._case_path("sample.txt")), 2) + self.assertEqual(count_safe_in_file(self._case_path("sample.txt")), 2) def test_all_safe(self): - self.assertEqual(self._count_safe_from_file(self._case_path("all_safe.txt")), 4) + self.assertEqual(count_safe_in_file(self._case_path("all_safe.txt")), 4) def test_all_unsafe(self): - self.assertEqual(self._count_safe_from_file(self._case_path("all_unsafe.txt")), 0) + self.assertEqual(count_safe_in_file(self._case_path("all_unsafe.txt")), 0) def test_edge_cases(self): - self.assertEqual(self._count_safe_from_file(self._case_path("edge_cases.txt")), 3) + self.assertEqual(count_safe_in_file(self._case_path("edge_cases.txt")), 3) if __name__ == "__main__": unittest.main() -