From e8a12f68aa551ea2c8df3003b0d4614457e2cfbb Mon Sep 17 00:00:00 2001 From: Luc Rubio Date: Sat, 7 Dec 2024 15:16:54 +0100 Subject: [PATCH] AoC2024 day 7 part 2 --- aoc2024/src/day07/python/solution.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/aoc2024/src/day07/python/solution.py b/aoc2024/src/day07/python/solution.py index df8a07c..f4f3017 100644 --- a/aoc2024/src/day07/python/solution.py +++ b/aoc2024/src/day07/python/solution.py @@ -25,7 +25,7 @@ def _could_be_true(equation_data: str, operators: tuple[str, ...]) -> int | None operands_queue = list(map(int, operands)) operators_queue = list(combination) computation = operands_queue.pop(0) - while len(operands_queue) and computation < result: + while len(operands_queue): next_operand = operands_queue.pop(0) next_operator = operators_queue.pop(0) # Cannot use eval() because it follows math precedence rules. @@ -35,6 +35,8 @@ def _could_be_true(equation_data: str, operators: tuple[str, ...]) -> int | None computation *= next_operand if next_operator == '||': computation = int(str(computation) + str(next_operand)) + if computation > result: + break # All operands increase computation so we can leave early. if computation == result: return result return None