from __future__ import annotations text: str with open("input.txt") as f: text = f.read() def toogle(current: str, button: list[int]): for index in button: current[index] = "." if current[index] == "#" else "#" def min_solver(pattern: str, buttons: list[list[int]], current: list[str], config: list[list[int]], min_solved: int) -> int: if len(config) == min_solved: return min_solved for button in buttons: if button in config: continue toogle(current, button) tmp = "".join(current) toogle(current, button) if tmp == pattern: return len(config) + 1 for button in buttons: if button in config: continue tmp = current.copy() toogle(tmp, button) config.append(button) min_solved = min(min_solved, min_solver(pattern, buttons, tmp, config, min_solved)) config.remove(button) return min_solved lines: list[str] = text.splitlines() total: int = 0 for i, line in enumerate(lines): splitter: int = line.index("]") buttons_str: list[str] = line[splitter + 2:line.rindex(" ")].split(" ") pattern: str = line[1:splitter] buttons: list[list[int]] = [list(map(int, button[1:-1].split(","))) for button in buttons_str] total += min_solver(pattern, buttons, ["." for _ in range(len(pattern))], [], len(buttons) * 2) print(i, len(lines)) print(total)