I know I have the answer.

This commit is contained in:
Ada Werefox 2025-12-12 18:39:27 -08:00
parent 876575e6a9
commit c581b51232

View file

@ -2,6 +2,7 @@
from logging import debug, DEBUG, basicConfig from logging import debug, DEBUG, basicConfig
from sys import argv from sys import argv
from re import finditer
class Manual: class Manual:
@ -10,24 +11,70 @@ class Manual:
count_solution: str count_solution: str
choices: list[str] choices: list[str]
def Line(self, input_line: str): def __init__(self, input_line: str):
self.light_solution = input_line.split()[0][1:-1] self.light_solution = input_line.split()[0][1:-1]
self.count_solution = input_line.split()[len(input_line) - 1][1:-1] self.count_solution = "".join(
input_line.split()[len(input_line.split()) - 1][1:-1]
)
self.choices = [x[1:-1] for x in input_line.split()[1:-1]] self.choices = [x[1:-1] for x in input_line.split()[1:-1]]
self.choices.sort(key=lambda x: len(x)) self.choices.sort(key=lambda x: len(x))
def find_solution(self): def find_solution(self) -> list[int]:
return debug("CURRENT CHOICES: %s", self.choices)
while self.count_solution != ",".join(["0" for _ in self.light_solution]):
# This needs to go back a step if we ever reach an empty best_options list...
# And iterate through the best options.
# This is probably best done with recursion.
debug("CURRENT COUNT: %s", self.count_solution)
minimum_value = str(
min([int(x) for x in self.count_solution.split(",") if int(x)])
)
minimum_indexes = [
str(i)
for i, v in enumerate(self.count_solution.split(","))
if v == minimum_value
]
debug(minimum_indexes)
best_options = [
len([v.string for v in c])
for c in [
finditer("|".join(minimum_indexes), c) for c in self.choices
]
]
debug(best_options)
solved_indexes = [
str(x)
for x in range(len(self.count_solution.split(",")))
if not int(self.count_solution.split(",")[x])
]
debug(solved_indexes)
for i, _ in enumerate(best_options):
for c in self.choices[i].split(","):
if c in solved_indexes:
best_options[i] = 0
debug(best_options)
current_choice = self.choices[best_options.index(max(best_options))]
debug(
"CURRENT CHOICE: %s",
current_choice,
)
for _ in range(int(minimum_value)):
for c in current_choice.split(","):
temp_count = self.count_solution.split(",")
temp_count[int(c)] = str(int(temp_count[int(c)]) - 1)
self.count_solution = ",".join(temp_count)
return []
lines: list[Line] lines: list[Line]
def Manual(self): def __init__(self):
self.lines = [] self.lines = []
def parse_input( def parse_input(
input_filepath: str, input_filepath: str,
) -> list[list[str]]: ) -> Manual:
with open(file=input_filepath, mode="r") as input_file: with open(file=input_filepath, mode="r") as input_file:
input_data: list[str] = input_file.readlines() input_data: list[str] = input_file.readlines()
@ -35,9 +82,11 @@ def parse_input(
debug(f"\n\nRAW INPUT: {input_data}\n\n") debug(f"\n\nRAW INPUT: {input_data}\n\n")
# Not even trying to be legible anymore. # Not even trying to be legible anymore.
output = [ # output = [
[segment[1:-1] for segment in line.strip().split()] for line in input_data # [segment[1:-1] for segment in line.strip().split()] for line in input_data
] # ]
output = Manual()
output.lines = [Manual.Line(l) for l in input_data]
return output return output
@ -79,12 +128,20 @@ def find_shortest_solutions(
def main() -> None: def main() -> None:
input_filepath = "input/test_manual.txt" input_filepath = "input/manual.txt"
input_manual = parse_input(input_filepath) input_manual = parse_input(input_filepath)
debug("MANUAL\n%s", input_manual) debug(
shortest_solutions = find_shortest_solutions(input_manual) "MANUAL\n%s",
debug(f"SHORTEST SOLUTIONS: {shortest_solutions}") [
print(f"The sum of shortest solution is: {sum(shortest_solutions)}") [line.light_solution, line.choices, line.count_solution]
for line in input_manual.lines
],
)
for line in input_manual.lines:
line.find_solution()
# shortest_solutions = find_shortest_solutions(input_manual)
# debug(f"SHORTEST SOLUTIONS: {shortest_solutions}")
# print(f"The sum of shortest solution is: {sum(shortest_solutions)}")
return return