add-day2 #1
@@ -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():
|
||||
def count_safe_in_file(path):
|
||||
return count_safe(parse_reports(path))
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser("CLI to read sample files.")
|
||||
parser.add_argument("input", help="Input file path (defaults to stdin). Use '-' for stdin.")
|
||||
parser.add_argument(
|
||||
"input",
|
||||
help="Input file path.",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
reports = parse_reports(args.input)
|
||||
args = parser.parse_args(argv)
|
||||
return count_safe_in_file(args.input)
|
||||
|
||||
print(count_safe(reports))
|
||||
|
||||
def cli(argv=None):
|
||||
print(main(argv))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
raise SystemExit(cli())
|
||||
|
||||
@@ -4,4 +4,3 @@
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user