2024 day 7.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Mikaël Capelle
2024-12-07 09:39:17 +01:00
parent 8c707c00ba
commit 1f5b21a13a
4 changed files with 915 additions and 2 deletions

View File

@@ -3,5 +3,48 @@ from typing import Any, Iterator
from ..base import BaseSolver
def evaluate(
target: int, numbers: list[int], concatenate: bool = False, current: int = 0
) -> bool:
if not numbers:
return current == target
if current > target:
return False
head, *tail = numbers
if evaluate(target, tail, concatenate, current + head) or evaluate(
target, tail, concatenate, current * head
):
return True
if not concatenate:
return False
return evaluate(target, tail, concatenate, int(str(current) + str(head)))
class Solver(BaseSolver):
def solve(self, input: str) -> Iterator[Any]: ...
def solve(self, input: str) -> Iterator[Any]:
targets = {
int(part[0]): list(map(int, part[1].strip().split()))
for line in input.splitlines()
if (part := line.split(":"))
}
yield sum(
target
for target, numbers in self.progress.wrap(
targets.items(), total=len(targets)
)
if evaluate(target, numbers)
)
yield sum(
target
for target, numbers in self.progress.wrap(
targets.items(), total=len(targets)
)
if evaluate(target, numbers, True)
)