More progress towards a rewrite.

This commit is contained in:
Ada Werefox 2025-12-12 16:28:02 -08:00
parent f4bc57fcd8
commit 876575e6a9

View file

@ -4,6 +4,27 @@ from logging import debug, DEBUG, basicConfig
from sys import argv from sys import argv
class Manual:
class Line:
light_solution: str
count_solution: str
choices: list[str]
def Line(self, input_line: str):
self.light_solution = input_line.split()[0][1:-1]
self.count_solution = input_line.split()[len(input_line) - 1][1:-1]
self.choices = [x[1:-1] for x in input_line.split()[1:-1]]
self.choices.sort(key=lambda x: len(x))
def find_solution(self):
return
lines: list[Line]
def Manual(self):
self.lines = []
def parse_input( def parse_input(
input_filepath: str, input_filepath: str,
) -> list[list[str]]: ) -> list[list[str]]:
@ -14,7 +35,9 @@ 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 = [[l[1:-1] for l in line.strip().split()] for line in input_data] output = [
[segment[1:-1] for segment in line.strip().split()] for line in input_data
]
return output return output
@ -30,52 +53,14 @@ def traverse_tree(solution: str, choices: list[str], states: set[str]) -> int:
for s in range(len(state)): for s in range(len(state)):
if str(s) in c.split(","): if str(s) in c.split(","):
if state[s] == ".": if state[s] == ".":
temp_state = temp_state[:s] + "#" + temp_state[s + 1 :] temp_state = temp_state[:s] + "#" + temp_state[s][1:]
else: else:
temp_state = temp_state[:s] + "." + temp_state[s + 1 :] temp_state = temp_state[:s] + "." + temp_state[s][1:]
new_states.add(temp_state) new_states.add(temp_state)
return 1 + traverse_tree(solution, choices, new_states) return 1 + traverse_tree(solution, choices, new_states)
def sum_of_states():
return
def press_button(button: str, state: str) -> str:
debug(f"\nSTATE: %s\nBUTTON: %s", state, button)
for light in range(len(state)):
if str(light) in button.split(","):
if state[light] == "#":
state = state[:light] + "." + state[light:][1:]
continue
state = state[:light] + "#" + state[light:][1:]
return state
def depth_traversal(
solution: str, choices: list[str], current: str, states: list[str]
) -> int:
debug(f"CURRENT STATE: %s", current)
debug(f"STATES_ ENCOUNTERED: %s", states)
for c in range(len(choices)):
new_state = press_button(choices[c], current)
debug(f"NEW STATE: %s", new_state)
if new_state == "." * len(solution):
return 0
if new_state == "".join(["#" if x % 2 == 1 else "." for x in solution]) and sum_of_states(states) == solution:
return 1
result = depth_traversal(
solution,
choices,
new_state,
states + [new_state],
)
if result == 1:
break
return 1 + result
def find_shortest_solutions( def find_shortest_solutions(
manual: list[list[str]], manual: list[list[str]],
) -> list[int]: ) -> list[int]:
@ -90,21 +75,6 @@ def find_shortest_solutions(
], ],
) )
output.append(traverse_tree(line[0], line[1:-1], {"".join("." * len(line[0]))})) output.append(traverse_tree(line[0], line[1:-1], {"".join("." * len(line[0]))}))
for line in manual:
solution = "".join(line[len(line) - 1].split(","))
debug(
"CURRENT SOLUTION: %s\nCURRENT CHOICES: %s",
solution,
[
"".join(["#" if str(x) in button else "." for x in range(len(line[0]))])
for button in line[1:-1]
],
)
output.append(
depth_traversal(
solution, line[1:-1], "." * len(solution), ["." * len(solution)]
)
)
return output return output