Adjust solvers to return integer

This commit is contained in:
Nettika
2023-12-16 16:44:55 -08:00
parent ffa9ebcf53
commit 54271e0a2c
7 changed files with 30 additions and 51 deletions

View File

@@ -7,7 +7,7 @@ from typing import Callable
from advent_of_code import cubes, trebuchet, gears from advent_of_code import cubes, trebuchet, gears
Solver = Callable[[str], str] Solver = Callable[[str], int]
solvers: dict[int, tuple[Solver, Solver]] = { solvers: dict[int, tuple[Solver, Solver]] = {

View File

@@ -77,18 +77,14 @@ class Game:
return config 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) actual_bag = Configuration(red=12, green=13, blue=14)
return str( return sum(
sum( game.id
game.id for game in Game.parse_table(input)
for game in Game.parse_table(input) if game.meets_configuration(actual_bag)
if game.meets_configuration(actual_bag)
)
) )
def solve_part_2(input: str) -> str: def solve_part_2(input: str) -> int:
return str( return sum(game.minimum_configuration().power() for game in Game.parse_table(input))
sum(game.minimum_configuration().power() for game in Game.parse_table(input))
)

View File

@@ -90,13 +90,12 @@ class Schematic:
return results return results
def solve_part_1(input: str) -> str: def solve_part_1(input: str) -> int:
schematic = Schematic.parse(input) schematic = Schematic.parse(input)
total = sum(int(part_number.number) for part_number, _ in schematic.part_numbers()) return sum(int(part_number.number) for part_number, _ in schematic.part_numbers())
return str(total)
def solve_part_2(input: str) -> str: def solve_part_2(input: str) -> int:
schematic = Schematic.parse(input) schematic = Schematic.parse(input)
part_groups: dict[SchematicSymbol, list[SchematicNumber]] = {} part_groups: dict[SchematicSymbol, list[SchematicNumber]] = {}
for part_number, part_symbol in schematic.part_numbers(): 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 part_numbers for part_numbers in part_groups.values() if len(part_numbers) == 2
] ]
total = 0 return sum(int(gear_1.number) * int(gear_2.number) for gear_1, gear_2 in gears)
for gear_1, gear_2 in gears:
gear_ratio = int(gear_1.number) * int(gear_2.number)
total += gear_ratio
return str(total)

View File

@@ -62,23 +62,19 @@ def _recover_all_calibration_values(
) )
def solve_part_1(input: str) -> str: def solve_part_1(input: str) -> int:
return str( return _recover_all_calibration_values(
_recover_all_calibration_values( input,
input, decimal_only_pattern,
decimal_only_pattern, decimal_only_pattern,
decimal_only_pattern, {},
{},
)
) )
def solve_part_2(input: str) -> str: def solve_part_2(input: str) -> int:
return str( return _recover_all_calibration_values(
_recover_all_calibration_values( input,
input, left_decimal_and_words_pattern,
left_decimal_and_words_pattern, right_decimal_and_words_pattern,
right_decimal_and_words_pattern, number_word_map,
number_word_map,
)
) )

View File

@@ -107,15 +107,8 @@ mock_input = "\n".join(
def test_solve_pt_1(): def test_solve_pt_1():
assert solve_part_1(mock_input) == "8" assert solve_part_1(mock_input) == 8
def test_solve_pt_2(): def test_solve_pt_2():
assert solve_part_2(mock_input) == "2286" 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]

View File

@@ -91,8 +91,8 @@ def test_schematic_part_numbers():
def test_solve_part_1(): def test_solve_part_1():
assert solve_part_1(mock_input) == "4361" assert solve_part_1(mock_input) == 4361
def test_solve_part_2(): def test_solve_part_2():
assert solve_part_2(mock_input) == "467835" assert solve_part_2(mock_input) == 467835

View File

@@ -54,7 +54,7 @@ def test_solve_part_1():
] ]
) )
) )
== "142" == 142
) )
@@ -73,5 +73,5 @@ def test_solve_part_2():
] ]
) )
) )
== "281" == 281
) )