Skip to content

Commit

Permalink
day 7 part 2. 59002246504791
Browse files Browse the repository at this point in the history
  • Loading branch information
OrsoEric committed Dec 8, 2024
1 parent 92cb0d3 commit ebca438
Show file tree
Hide file tree
Showing 2 changed files with 566 additions and 4 deletions.
19 changes: 15 additions & 4 deletions solutions/orso/day07/day_7.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class Equation:
n_num_arg : int = 0
ln_arguments : List[int] = list()
s_operators : str = str()
#PLUS and MUL
#cs_operations = "+*"
#PLUS MUL AND PIPE
cs_operations = "+*|"

def set( self, in_result: int, iln_argument : List[int] ) -> bool:
self.n_result = in_result
Expand All @@ -62,7 +66,7 @@ def generate_operators(self, in_num_operators) -> List[List[str]]:
"""
if in_num_operators <= 0:
return list() #FAIL
combinations = list(product("+*", repeat=in_num_operators))
combinations = list(product("{self.cs_operations}", repeat=in_num_operators))
lln_operators = [''.join(combo) for combo in combinations]

return lln_operators
Expand All @@ -75,7 +79,7 @@ def operation_generator(self, in_num_operators) -> Generator[str, None, None]:
if in_num_operators <= 0:
return list() #FAIL

combinations = list(product("+*", repeat=in_num_operators))
combinations = list(product(f"{self.cs_operations}", repeat=in_num_operators))
for combo in combinations:
yield ''.join(combo)

Expand Down Expand Up @@ -105,6 +109,10 @@ def evaluate(self, s_operators : str ) -> int:
n_accumulator += self.ln_arguments[n_index+1]
elif s_operator == "*":
n_accumulator *= self.ln_arguments[n_index+1]
#pipe operator will concatenate a number of arguments to the accumulator
elif s_operator == "|":
s_concatenate = f"{n_accumulator}{self.ln_arguments[n_index+1]}"
n_accumulator = int(s_concatenate)
else:
logging.error("ERROR: invalid operator {s_operator}")
return 0
Expand Down Expand Up @@ -177,7 +185,8 @@ def show(self):

def solve(self) -> int:
n_accumulator_result = 0
for st_equation in self.glst_equation:
for n_index, st_equation in enumerate(self.glst_equation):
print(f"solving {n_index} of {len(self.glst_equation)}")
logging.debug("SOLVE:")
st_equation.show()
b_fail = st_equation.solve()
Expand All @@ -198,6 +207,7 @@ def save_results( self, s_filename : str ) -> bool:
file.write(s_equation + '\n')
except Exception as e:
logging.error(f"Save Failed: {e}")

#--------------------------------------------------------------------------------------------------------------------------------
# MAIN
#--------------------------------------------------------------------------------------------------------------------------------
Expand All @@ -223,5 +233,6 @@ def save_results( self, s_filename : str ) -> bool:
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')
#cl_operator_finder.save_results('day07\day_7_output_part_1.txt')
cl_operator_finder.save_results('day07\day_7_output_part_2.txt')
print(f"accumulator: {n_accumulator_result}")
Loading

0 comments on commit ebca438

Please sign in to comment.