Python: Day 3

This commit is contained in:
Adrian Rumpold
2022-12-15 08:00:17 +01:00
parent 7237b2c770
commit aeebed7ec9
4 changed files with 492 additions and 0 deletions

57
03/py/test.py Normal file
View File

@@ -0,0 +1,57 @@
from typing import ContextManager, Optional
from solution import part1, part2, priority, threes
from contextlib import nullcontext as no_raise
import pytest
@pytest.fixture
def example_data() -> str:
return """vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw"""
def test_part1(example_data: str) -> None:
assert part1(example_data) == 157
@pytest.mark.parametrize(
"s,expected",
[
("a", 1),
("b", 2),
("z", 26),
("A", 27),
("C", 29),
("Z", 52),
],
)
def test_priority(s: str, expected: int) -> None:
assert priority(s) == expected
@pytest.mark.parametrize(
"line,expected,raises",
[
("a\nb\nc", [("a", "b", "c")], no_raise()),
("a\nb\nc\nd", None, pytest.raises(ValueError)),
],
)
def test_threes(
line: str,
expected: Optional[list[tuple[str, str, str]]],
raises: ContextManager,
):
with raises:
actual = list(threes(line.splitlines()))
if expected:
assert actual == expected
def test_part2(example_data: str) -> None:
assert part2(example_data) == 70