aoc-2025/10/main.py

126 lines
3.7 KiB
Python
Raw Normal View History

2025-12-10 23:19:35 -06:00
#!/usr/bin/env python3
from logging import debug, DEBUG, basicConfig
from sys import argv
def parse_input(
input_filepath: str,
2025-12-12 00:24:27 -06:00
) -> list[list[str]]:
2025-12-10 23:19:35 -06:00
with open(file=input_filepath, mode="r") as input_file:
input_data: list[str] = input_file.readlines()
debug(f"\n\nRAW INPUT: {input_data}\n\n")
# Not even trying to be legible anymore.
2025-12-12 00:24:27 -06:00
output = [[l[1:-1] for l in line.strip().split()] for line in input_data]
return output
2025-12-10 23:19:35 -06:00
2025-12-12 00:24:27 -06:00
def traverse_tree(solution: str, choices: list[str], states: set[str]) -> int:
debug("CURRENT STATES: %s", states)
2025-12-10 23:19:35 -06:00
if solution in states:
debug("FOUND IT")
return 0
2025-12-12 00:24:27 -06:00
new_states: set[str] = set()
2025-12-10 23:19:35 -06:00
for state in states:
2025-12-12 00:24:27 -06:00
for c in choices:
temp_state = state
for s in range(len(state)):
if str(s) in c.split(","):
if state[s] == ".":
temp_state = temp_state[:s] + "#" + temp_state[s + 1 :]
else:
temp_state = temp_state[:s] + "." + temp_state[s + 1 :]
new_states.add(temp_state)
2025-12-10 23:19:35 -06:00
return 1 + traverse_tree(solution, choices, new_states)
2025-12-12 01:44:14 -06:00
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
2025-12-10 23:19:35 -06:00
def find_shortest_solutions(
2025-12-12 00:24:27 -06:00
manual: list[list[str]],
2025-12-10 23:19:35 -06:00
) -> list[int]:
output: list[int] = []
for line in manual:
debug(
"CURRENT SOLUTION: %s\nCURRENT CHOICES: %s",
2025-12-12 00:24:27 -06:00
line[0],
[
"".join(["#" if str(x) in button else "." for x in range(len(line[0]))])
for button in line[1:-1]
],
2025-12-10 23:19:35 -06:00
)
2025-12-12 00:24:27 -06:00
output.append(traverse_tree(line[0], line[1:-1], {"".join("." * len(line[0]))}))
2025-12-12 01:44:14 -06:00
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)]
)
)
2025-12-10 23:19:35 -06:00
return output
def main() -> None:
2025-12-12 01:44:14 -06:00
input_filepath = "input/test_manual.txt"
2025-12-10 23:19:35 -06:00
input_manual = parse_input(input_filepath)
2025-12-12 00:24:27 -06:00
debug("MANUAL\n%s", input_manual)
2025-12-10 23:19:35 -06:00
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
if __name__ == "__main__":
if "-d" in argv or "--debug" in argv:
basicConfig(filename="debug.log", level=DEBUG)
main()
exit(0)