add-day2 #1

Merged
Josh merged 4 commits from add-day2 into main 2026-05-19 21:31:44 +00:00
3 changed files with 35 additions and 34 deletions
Showing only changes of commit d782d5e662 - Show all commits

View File

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

View File

@@ -1,7 +1,6 @@
7 6 4 2 1 7 6 4 2 1
1 2 7 8 9 1 2 7 8 9
9 7 6 2 1 9 7 6 2 1
1 3 2 4 5 1 3 2 4 5
8 6 4 4 1 8 6 4 4 1
1 3 6 7 9 1 3 6 7 9

View File

@@ -1,32 +1,26 @@
import os import os
import unittest import unittest
from day2 import aoc_2024_day2 from day2_sample import count_safe_in_file
class TestDay2(unittest.TestCase): 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): def _case_path(self, filename):
here = os.path.dirname(__file__) here = os.path.dirname(__file__)
return os.path.join(here, "test_cases", filename) return os.path.join(here, "test_cases", filename)
def test_sample(self): 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): 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): 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): 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__": if __name__ == "__main__":
unittest.main() unittest.main()