From 54271e0a2c70744ec106539b6ef2823f59de392c Mon Sep 17 00:00:00 2001 From: Nettika Date: Sat, 16 Dec 2023 16:44:55 -0800 Subject: [PATCH] Adjust solvers to return integer --- advent_of_code/__init__.py | 2 +- advent_of_code/cubes.py | 18 +++++++----------- advent_of_code/gears.py | 14 ++++---------- advent_of_code/trebuchet.py | 28 ++++++++++++---------------- tests/cubes_test.py | 11 ++--------- tests/gears_test.py | 4 ++-- tests/trebuchet_test.py | 4 ++-- 7 files changed, 30 insertions(+), 51 deletions(-) diff --git a/advent_of_code/__init__.py b/advent_of_code/__init__.py index 5835b4e..25c9de1 100644 --- a/advent_of_code/__init__.py +++ b/advent_of_code/__init__.py @@ -7,7 +7,7 @@ from typing import Callable from advent_of_code import cubes, trebuchet, gears -Solver = Callable[[str], str] +Solver = Callable[[str], int] solvers: dict[int, tuple[Solver, Solver]] = { diff --git a/advent_of_code/cubes.py b/advent_of_code/cubes.py index 2e15ff6..85d6757 100644 --- a/advent_of_code/cubes.py +++ b/advent_of_code/cubes.py @@ -77,18 +77,14 @@ class Game: return config -def solve_part_1(input: str) -> str: +def solve_part_1(input: str) -> int: actual_bag = Configuration(red=12, green=13, blue=14) - return str( - sum( - game.id - for game in Game.parse_table(input) - if game.meets_configuration(actual_bag) - ) + return sum( + game.id + for game in Game.parse_table(input) + if game.meets_configuration(actual_bag) ) -def solve_part_2(input: str) -> str: - return str( - sum(game.minimum_configuration().power() for game in Game.parse_table(input)) - ) +def solve_part_2(input: str) -> int: + return sum(game.minimum_configuration().power() for game in Game.parse_table(input)) diff --git a/advent_of_code/gears.py b/advent_of_code/gears.py index 5cac4ba..97a316d 100644 --- a/advent_of_code/gears.py +++ b/advent_of_code/gears.py @@ -90,13 +90,12 @@ class Schematic: return results -def solve_part_1(input: str) -> str: +def solve_part_1(input: str) -> int: schematic = Schematic.parse(input) - total = sum(int(part_number.number) for part_number, _ in schematic.part_numbers()) - return str(total) + return sum(int(part_number.number) for part_number, _ in schematic.part_numbers()) -def solve_part_2(input: str) -> str: +def solve_part_2(input: str) -> int: schematic = Schematic.parse(input) part_groups: dict[SchematicSymbol, list[SchematicNumber]] = {} for part_number, part_symbol in schematic.part_numbers(): @@ -109,9 +108,4 @@ def solve_part_2(input: str) -> str: part_numbers for part_numbers in part_groups.values() if len(part_numbers) == 2 ] - total = 0 - for gear_1, gear_2 in gears: - gear_ratio = int(gear_1.number) * int(gear_2.number) - total += gear_ratio - - return str(total) + return sum(int(gear_1.number) * int(gear_2.number) for gear_1, gear_2 in gears) diff --git a/advent_of_code/trebuchet.py b/advent_of_code/trebuchet.py index 7d0f183..6403a63 100644 --- a/advent_of_code/trebuchet.py +++ b/advent_of_code/trebuchet.py @@ -62,23 +62,19 @@ def _recover_all_calibration_values( ) -def solve_part_1(input: str) -> str: - return str( - _recover_all_calibration_values( - input, - decimal_only_pattern, - decimal_only_pattern, - {}, - ) +def solve_part_1(input: str) -> int: + return _recover_all_calibration_values( + input, + decimal_only_pattern, + decimal_only_pattern, + {}, ) -def solve_part_2(input: str) -> str: - return str( - _recover_all_calibration_values( - input, - left_decimal_and_words_pattern, - right_decimal_and_words_pattern, - number_word_map, - ) +def solve_part_2(input: str) -> int: + return _recover_all_calibration_values( + input, + left_decimal_and_words_pattern, + right_decimal_and_words_pattern, + number_word_map, ) diff --git a/tests/cubes_test.py b/tests/cubes_test.py index 0d73f4c..e226791 100644 --- a/tests/cubes_test.py +++ b/tests/cubes_test.py @@ -107,15 +107,8 @@ mock_input = "\n".join( def test_solve_pt_1(): - assert solve_part_1(mock_input) == "8" + assert solve_part_1(mock_input) == 8 def test_solve_pt_2(): - assert solve_part_2(mock_input) == "2286" - - -# def test_configuration_power(): -# assert [ -# _configuration_power(Game.parse(game_desc).minimum_configuration()) -# for game_desc in mock_input -# ] == [48, 12, 1560, 630, 36] + assert solve_part_2(mock_input) == 2286 diff --git a/tests/gears_test.py b/tests/gears_test.py index 12880f0..d234b8d 100644 --- a/tests/gears_test.py +++ b/tests/gears_test.py @@ -91,8 +91,8 @@ def test_schematic_part_numbers(): def test_solve_part_1(): - assert solve_part_1(mock_input) == "4361" + assert solve_part_1(mock_input) == 4361 def test_solve_part_2(): - assert solve_part_2(mock_input) == "467835" + assert solve_part_2(mock_input) == 467835 diff --git a/tests/trebuchet_test.py b/tests/trebuchet_test.py index 90d8975..2185b36 100644 --- a/tests/trebuchet_test.py +++ b/tests/trebuchet_test.py @@ -54,7 +54,7 @@ def test_solve_part_1(): ] ) ) - == "142" + == 142 ) @@ -73,5 +73,5 @@ def test_solve_part_2(): ] ) ) - == "281" + == 281 )