-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart_1.py
77 lines (60 loc) · 1.9 KB
/
part_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from typing import Callable, List, Optional, Tuple
def parseInputFile() -> List[Tuple[int, List[int]]]:
"""
Parse the input file to extract calibration equations.
Each line is formatted as `result: term1 term2 ...`.
"""
with open("./input.txt", "r") as file:
lines = file.read().strip().splitlines()
calibration_equations = []
for line in lines:
parts = line.split(":")
result = int(parts[0].strip())
terms = list(map(int, parts[1].strip().split()))
calibration_equations.append((result, terms))
return calibration_equations
def add(acc: Optional[int], term: int) -> int:
"""
Add operator: sum the accumulator and the current term.
"""
return (acc or 0) + term
def multiply(acc: Optional[int], term: int) -> int:
"""
Multiply operator: multiply the accumulator and the current term.
"""
return (acc or 1) * term
def concatenate(acc: Optional[int], term: int) -> int:
"""
Concatenate operator: append the current term to the accumulator.
"""
if acc is None:
return term
return int(f"{acc}{term}")
def validateEquation(
result: int,
terms: List[int],
acc: Optional[int],
operators: List[Callable[[Optional[int], int], int]],
) -> bool:
"""
Recursively validate whether the result can be achieved using the terms
and allowed operators.
"""
# Base case
if not terms:
return result == acc
# Recursive case: try all operators
return any(
validateEquation(result, terms[1:], op(acc, terms[0]), operators)
for op in operators
)
def totalEquationCalibration() -> int:
"""
Solve part 1: Use add and multiply operators.
"""
calibration_equations = parseInputFile()
return sum(
result
for result, terms in calibration_equations
if validateEquation(result, terms, None, [add, multiply])
)