58 lines
1.3 KiB
Python
58 lines
1.3 KiB
Python
import itertools
|
|
import re
|
|
|
|
def press_buttons(buttons, lights):
|
|
|
|
for button in buttons:
|
|
for light in button:
|
|
|
|
if lights[int(light)] == ".":
|
|
lights[int(light)] = "#"
|
|
else:
|
|
lights[int(light)] = "."
|
|
|
|
return "".join(lights)
|
|
|
|
|
|
def read_input(filename):
|
|
manuals = []
|
|
for line in open(filename).read().splitlines():
|
|
|
|
lights = re.findall(r"\[(.*?)\]",line)[0]
|
|
buttons = re.findall(r"\((.*?)\)",line)
|
|
|
|
buttons = [ b.split(",") for b in buttons ]
|
|
|
|
manuals.append((lights,buttons))
|
|
return manuals
|
|
|
|
|
|
# filename = "day10/example_input" # 7
|
|
filename = "day10/input" # 419
|
|
|
|
manuals = read_input(filename)
|
|
|
|
buttons_pressed = []
|
|
for manual in manuals:
|
|
presses = 0
|
|
|
|
target_lights = manual[0]
|
|
continueSearch = True
|
|
while continueSearch:
|
|
presses += 1
|
|
for buttons in itertools.combinations(manual[1],presses):
|
|
|
|
lights = press_buttons(buttons, ["."] * len(target_lights))
|
|
|
|
if lights == target_lights:
|
|
buttons_pressed.append(len(buttons))
|
|
continueSearch = False
|
|
break
|
|
|
|
sum_buttons = 0
|
|
for butt in buttons_pressed:
|
|
sum_buttons += butt
|
|
|
|
|
|
print(f"Sum of fewest buttons: {sum_buttons}")
|