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
Solver = Callable[[str], str]
Solver = Callable[[str], int]
solvers: dict[int, tuple[Solver, Solver]] = {

View File

@@ -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))

View File

@@ -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)

View File

@@ -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,
)