Skip to content

Commit

Permalink
day2: coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ong committed Dec 26, 2023
1 parent 437306f commit af48a25
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 18 deletions.
8 changes: 6 additions & 2 deletions day01/day1a.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ def get_input(input_file: str) -> list[str]:
return list(file)


def main(input_file: str = INPUT) -> int:
lines = get_input(input_file)
def part1(lines: list[str]) -> int:
total = 0
for line in lines:
first, last = get_first_last(line)
Expand All @@ -36,5 +35,10 @@ def main(input_file: str = INPUT) -> int:
return total


def main() -> None:
lines = get_input(INPUT)
print(part1(lines))


if __name__ == "__main__":
main()
13 changes: 8 additions & 5 deletions day01/day1b.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class WordNumber:
number: str = "1"


mappings = [
MAPPINGS: list[WordNumber] = [
WordNumber("one", "1"),
WordNumber("two", "2"),
WordNumber("three", "3"),
Expand All @@ -43,7 +43,7 @@ def process_line(line: str) -> int:
if char.isnumeric():
index_to_chars[index] = char

for mapping in mappings:
for mapping in MAPPINGS:
substring = line[index : index + len(mapping.word)]
if substring == mapping.word:
index_to_chars[index] = mapping.number
Expand All @@ -60,12 +60,15 @@ def get_input(input_file: str) -> list[str]:
return list(file)


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


def main() -> None:
lines = get_input(INPUT)
print(part2(lines))


if __name__ == "__main__":
main()
7 changes: 4 additions & 3 deletions day01/tests/test_day1a.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

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


def test_get_first_last() -> None:
Expand All @@ -17,8 +17,9 @@ def test_get_first_last() -> None:
get_first_last("")


def test_main() -> None:
assert main(INPUT_SMALL) == 142
def test_part1() -> None:
lines: list[str] = get_input(INPUT_SMALL)
assert part1(lines) == 142


def test_get_input() -> None:
Expand Down
7 changes: 4 additions & 3 deletions day01/tests/test_day1b.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from day01.day1b import INPUT_SMALL, IndexValue, get_input, main, process_line
from day01.day1b import INPUT_SMALL, IndexValue, get_input, part2, process_line


def test_process_line() -> None:
Expand All @@ -23,5 +23,6 @@ def test_index_value() -> None:
assert str(index_val) == ("(i:0, v:1)")


def test_main() -> None:
assert main(INPUT_SMALL) == 281
def test_part2() -> None:
lines: list[str] = get_input(INPUT_SMALL)
assert part2(lines) == 281
5 changes: 1 addition & 4 deletions day02/day2.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def parse_colors_count(self, string: str) -> None:
self.red = num
elif color == Color.GREEN:
self.green = num
elif color == Color.BLUE:
else: # color == Color.BLUE:
self.blue = num


Expand Down Expand Up @@ -66,9 +66,6 @@ def __str__(self) -> str:
def power_level(self) -> int:
return self.red * self.green * self.blue

def __repr__(self) -> str:
return str(self)


def game_filter(game: Game) -> bool:
"""Returns true if the game satisfies the constraints of Question 1"""
Expand Down
5 changes: 5 additions & 0 deletions day02/tests/test_day2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ def test_draw() -> None:
"""Test draws"""
draw1: Draw = Draw("3 blue, 4 red")
draw2: Draw = Draw("4 blue, 3 green")
draw3: Draw = Draw("4 blue, 3 green, 1 red")

assert [draw1.red, draw1.green, draw1.blue] == [4, 0, 3]
assert [draw2.red, draw2.green, draw2.blue] == [0, 3, 4]
assert [draw3.red, draw3.green, draw3.blue] == [1, 3, 4]


def test_game() -> None:
Expand All @@ -33,6 +35,9 @@ def test_game() -> None:
assert game2.green == 3
assert game2.blue == 4

assert str(game1) == "Game 1: 4,2,6"
assert str(game2) == "Game 2: 1,3,4"


def test_part1() -> None:
"""Tests get_games, game_filter, part1"""
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ command_line = '-m pytest'
source = ['.']
relative_files = true


[tool.coverage.report]
exclude_lines = ["if __name__ == .__main__.:"]
exclude_lines = ["if __name__ == .__main__.:", "def main"]
omit = ["download_inputs.py", "maker.py"]
precision = 2
skip_covered = true
fail_under = 0

0 comments on commit af48a25

Please sign in to comment.