#!/usr/bin/env python3 from logging import debug, DEBUG, basicConfig 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( input_filepath: str, ) -> list[list[str]]: 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. output = [ [segment[1:-1] for segment in line.strip().split()] for line in input_data ] return output def traverse_tree(solution: str, choices: list[str], states: set[str]) -> int: debug("CURRENT STATES: %s", states) if solution in states: debug("FOUND IT") return 0 new_states: set[str] = set() for state in states: 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) return 1 + traverse_tree(solution, choices, new_states) def find_shortest_solutions( manual: list[list[str]], ) -> list[int]: output: list[int] = [] for line in manual: debug( "CURRENT SOLUTION: %s\nCURRENT CHOICES: %s", line[0], [ "".join(["#" if str(x) in button else "." for x in range(len(line[0]))]) for button in line[1:-1] ], ) output.append(traverse_tree(line[0], line[1:-1], {"".join("." * len(line[0]))})) return output def main() -> None: input_filepath = "input/test_manual.txt" input_manual = parse_input(input_filepath) debug("MANUAL\n%s", input_manual) 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)