Skip to content

Commit

Permalink
day01: coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ong committed Dec 26, 2023
1 parent c043a7a commit 437306f
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 18 deletions.
8 changes: 6 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ repos:
rev: v0.1.8
hooks:
- id: ruff
stages: [commit, manual]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0 # Use the sha / tag you want to point at
hooks:
- id: mypy
stages: [commit, manual]
args:
- "--check-untyped-defs"
- "--strict"
Expand All @@ -19,6 +21,7 @@ repos:
types-requests==2.31.0.10,
types-tqdm==4.66.0.5,
python-dotenv==1.0.0,
pytest==7.4.3,
graphviz==0.20.1,
vpython==7.6.3,
z3-solver==4.12.4.0,
Expand All @@ -27,21 +30,22 @@ repos:
rev: v4.5.0
hooks:
- id: mixed-line-ending
stages: [commit, manual]
args:
- "--fix=lf"
- repo: local
hooks:
- id: pytest-check
name: pytest-check
stages: [pre-push]
stages: [pre-push, manual]
types: [python]
entry: pytest -v --color=yes
language: system
always_run: true
pass_filenames: false
- id: coverage-check # technically runs pytest again. oh well
name: coverage-check
stages: [pre-push]
stages: [pre-push, manual]
types: [python]
entry: coverage run
language: system
Expand Down
24 changes: 16 additions & 8 deletions day01/day1a.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

from typing import Optional

INPUT = "day01/input.txt"
INPUT_SMALL = "day01/input-small.txt"


def get_first_last(line: str) -> tuple[str, str]:
first: Optional[str] = None
Expand All @@ -18,14 +21,19 @@ def get_first_last(line: str) -> tuple[str, str]:
return (first, last)


def main() -> None:
with open("day01/input.txt", "r", encoding="utf8") as file:
total = 0
for line in file:
first, last = get_first_last(line)
data = int(first + last)
total += data
print(total)
def get_input(input_file: str) -> list[str]:
with open(input_file, "r", encoding="utf8") as file:
return list(file)


def main(input_file: str = INPUT) -> int:
lines = get_input(input_file)
total = 0
for line in lines:
first, last = get_first_last(line)
data = int(first + last)
total += data
return total


if __name__ == "__main__":
Expand Down
18 changes: 12 additions & 6 deletions day01/day1b.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

from dataclasses import dataclass

INPUT = "day01/input.txt"
INPUT_SMALL = "day01/input-small2.txt"


@dataclass
class IndexValue:
Expand Down Expand Up @@ -52,13 +55,16 @@ def process_line(line: str) -> int:
return int(index_to_chars[first_index] + index_to_chars[last_index])


def main() -> None:
with open("day01/input.txt", "r", encoding="utf8") as file:
total = 0
for line in file:
total += process_line(line)
def get_input(input_file: str) -> list[str]:
with open(input_file, "r", encoding="utf8") as file:
return list(file)


print(total)
def main(input_file: str = INPUT) -> int:
lines = get_input(input_file)
total = sum(process_line(line) for line in lines)
print(total)
return total


if __name__ == "__main__":
Expand Down
4 changes: 4 additions & 0 deletions day01/input-small.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
7 changes: 7 additions & 0 deletions day01/input-small2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
17 changes: 16 additions & 1 deletion day01/tests/test_day1a.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from day01.day1a import get_first_last
import pytest

from day01.day1a import INPUT_SMALL, get_first_last, get_input, main


def test_get_first_last() -> None:
Expand All @@ -10,3 +12,16 @@ def test_get_first_last() -> None:
assert get_first_last("pqr3stu8vwx") == ("3", "8")
assert get_first_last("a1b2c3d4e5f") == ("1", "5")
assert get_first_last("treb7uchet") == ("7", "7")

with pytest.raises(ValueError):
get_first_last("")


def test_main() -> None:
assert main(INPUT_SMALL) == 142


def test_get_input() -> None:
lines: list[str] = get_input(INPUT_SMALL)
assert len(lines) == 4
assert lines[0].strip() == "1abc2"
17 changes: 16 additions & 1 deletion day01/tests/test_day1b.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from day01.day1b import process_line
from day01.day1b import INPUT_SMALL, IndexValue, get_input, main, process_line


def test_process_line() -> None:
Expand All @@ -10,3 +10,18 @@ def test_process_line() -> None:
assert process_line("zoneight234") == 14
assert process_line("7pqrstsixteen") == 76
assert process_line("zoneight") == 18


def test_get_input() -> None:
lines: list[str] = get_input(INPUT_SMALL)
assert len(lines) == 7
assert lines[0].strip() == "two1nine"


def test_index_value() -> None:
index_val: IndexValue = IndexValue(0, "1")
assert str(index_val) == ("(i:0, v:1)")


def test_main() -> None:
assert main(INPUT_SMALL) == 281
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ source = ['.']
relative_files = true

[tool.coverage.report]
exclude_lines = ["if __name__ == .__main__.:"]
precision = 2
skip_covered = true
fail_under = 0

0 comments on commit 437306f

Please sign in to comment.