I need to go to bed.
This commit is contained in:
parent
435d658886
commit
f4bc57fcd8
1 changed files with 54 additions and 1 deletions
55
10/main.py
55
10/main.py
|
|
@ -38,6 +38,44 @@ def traverse_tree(solution: str, choices: list[str], states: set[str]) -> int:
|
||||||
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]:
|
||||||
|
|
@ -52,11 +90,26 @@ 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
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
input_filepath = "input/manual.txt"
|
input_filepath = "input/test_manual.txt"
|
||||||
input_manual = parse_input(input_filepath)
|
input_manual = parse_input(input_filepath)
|
||||||
debug("MANUAL\n%s", input_manual)
|
debug("MANUAL\n%s", input_manual)
|
||||||
shortest_solutions = find_shortest_solutions(input_manual)
|
shortest_solutions = find_shortest_solutions(input_manual)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue