Skip to content

Commit

Permalink
save to file the valid equations with valid operators.
Browse files Browse the repository at this point in the history
  • Loading branch information
OrsoEric committed Dec 8, 2024
1 parent 70d03b2 commit 92cb0d3
Show file tree
Hide file tree
Showing 3 changed files with 426 additions and 1 deletion.
25 changes: 24 additions & 1 deletion solutions/orso/day07/day_7.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ def solve(self) -> bool:
b_continue = False #solution not found
return True #FAIL

def get_equation_string(self)->str:
"""
generate a string of the equation: "5=3+2"
"""
if len(self.s_operators) <= 0:
return str()
s_result = f"{self.n_result}={self.ln_arguments[0]}"
for n_index, s_operator in enumerate(self.s_operators):
s_result += f" {s_operator} {self.ln_arguments[n_index+1]}"
logging.debug(f"{s_result}")
return s_result

def __init__(self):
"""
Initialize the Patrol_route class
Expand Down Expand Up @@ -175,7 +187,17 @@ def solve(self) -> int:
else:
logging.debug(f"NOT SOLVABLE:")
return n_accumulator_result


def save_results( self, s_filename : str ) -> bool:
try:
with open(s_filename, 'w') as file:
for st_equation in self.glst_equation:
s_equation = st_equation.get_equation_string()
print(s_equation)
if (len(s_equation) > 0):
file.write(s_equation + '\n')
except Exception as e:
logging.error(f"Save Failed: {e}")
#--------------------------------------------------------------------------------------------------------------------------------
# MAIN
#--------------------------------------------------------------------------------------------------------------------------------
Expand All @@ -201,4 +223,5 @@ def solve(self) -> int:
cl_operator_finder.load_equations(gs_filename_data)
cl_operator_finder.show()
n_accumulator_result = cl_operator_finder.solve()
cl_operator_finder.save_results('day07\day_7_output_part_1.txt')
print(f"accumulator: {n_accumulator_result}")
22 changes: 22 additions & 0 deletions solutions/orso/day07/day_7_formulation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,27 @@ In the above example, the sum of the test values for the three equations listed

Determine which equations could possibly be true. What is their total calibration result?

--- Part Two ---

The engineers seem concerned;
the total calibration result you gave them is nowhere close to being within safety tolerances.
Just then, you spot your mistake: some well-hidden elephants are holding a third type of operator.

The concatenation operator (||) combines the digits from its left and right inputs into a single number.
For example, 12 || 345 would become 12345. All operators are still evaluated left-to-right.

Now, apart from the three equations that could be made true using only addition and multiplication,
the above example has three more equations that can be made true by inserting operators:

156: 15 6 can be made true through a single concatenation: 15 || 6 = 156.
7290: 6 8 6 15 can be made true using 6 * 8 || 6 * 15.
192: 17 8 14 can be made true using 17 || 8 + 14.

Adding up all six test values (the three that could be made before using only + and * plus the new three that can now be made by also using ||)
produces the new total calibration result of 11387.

Using your new knowledge of elephant hiding spots, determine which equations could possibly be true. What is their total calibration result?




Loading

0 comments on commit 92cb0d3

Please sign in to comment.