Skip to content

Commit

Permalink
AoC2024 day 7 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
loociano committed Dec 7, 2024
1 parent 0fcf749 commit e8a12f6
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion aoc2024/src/day07/python/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit e8a12f6

Please sign in to comment.