Refactor.
This commit is contained in:
parent
b9ce8890cb
commit
435d658886
1 changed files with 23 additions and 35 deletions
58
10/main.py
58
10/main.py
|
|
@ -6,7 +6,7 @@ from sys import argv
|
||||||
|
|
||||||
def parse_input(
|
def parse_input(
|
||||||
input_filepath: str,
|
input_filepath: str,
|
||||||
) -> list[tuple[list[bool], list[list[bool]], list[int]]]:
|
) -> list[list[str]]:
|
||||||
|
|
||||||
with open(file=input_filepath, mode="r") as input_file:
|
with open(file=input_filepath, mode="r") as input_file:
|
||||||
input_data: list[str] = input_file.readlines()
|
input_data: list[str] = input_file.readlines()
|
||||||
|
|
@ -14,63 +14,51 @@ def parse_input(
|
||||||
debug(f"\n\nRAW INPUT: {input_data}\n\n")
|
debug(f"\n\nRAW INPUT: {input_data}\n\n")
|
||||||
|
|
||||||
# Not even trying to be legible anymore.
|
# Not even trying to be legible anymore.
|
||||||
return [
|
output = [[l[1:-1] for l in line.strip().split()] for line in input_data]
|
||||||
(
|
return output
|
||||||
[l == "#" for l in line.split()[0][1:-1]],
|
|
||||||
[
|
|
||||||
[
|
|
||||||
str(w) in l[1:-1].split(",")
|
|
||||||
for w in range(len(line.split()[0][1:-1]))
|
|
||||||
]
|
|
||||||
for l in line.split()[1:-1]
|
|
||||||
if line
|
|
||||||
],
|
|
||||||
[int(j) for j in line.split()[-1][1:-1].split(",")],
|
|
||||||
)
|
|
||||||
for line in input_data
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def traverse_tree(
|
def traverse_tree(solution: str, choices: list[str], states: set[str]) -> int:
|
||||||
solution: list[bool], choices: list[list[bool]], states: list[list[bool]]
|
debug("CURRENT STATES: %s", states)
|
||||||
) -> int:
|
|
||||||
debug(
|
|
||||||
"CURRENT STATES: %s",
|
|
||||||
["".join(["#" if x else "." for x in s]) for s in states],
|
|
||||||
)
|
|
||||||
if solution in states:
|
if solution in states:
|
||||||
debug("FOUND IT")
|
debug("FOUND IT")
|
||||||
return 0
|
return 0
|
||||||
new_states: list[list[bool]] = []
|
new_states: set[str] = set()
|
||||||
for state in states:
|
for state in states:
|
||||||
for choice_index in range(len(choices)):
|
for c in choices:
|
||||||
temp_state = state.copy()
|
temp_state = state
|
||||||
for c in range(len(choices[choice_index])):
|
for s in range(len(state)):
|
||||||
if choices[choice_index][c]:
|
if str(s) in c.split(","):
|
||||||
temp_state[c] = not temp_state[c]
|
if state[s] == ".":
|
||||||
new_states.append(temp_state)
|
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)
|
return 1 + traverse_tree(solution, choices, new_states)
|
||||||
|
|
||||||
|
|
||||||
def find_shortest_solutions(
|
def find_shortest_solutions(
|
||||||
manual: list[tuple[list[bool], list[list[bool]], list[int]]],
|
manual: list[list[str]],
|
||||||
) -> list[int]:
|
) -> list[int]:
|
||||||
output: list[int] = []
|
output: list[int] = []
|
||||||
for line in manual:
|
for line in manual:
|
||||||
debug(
|
debug(
|
||||||
"CURRENT SOLUTION: %s\nCURRENT CHOICES: %s",
|
"CURRENT SOLUTION: %s\nCURRENT CHOICES: %s",
|
||||||
"".join(["#" if x else "." for x in line[0]]),
|
line[0],
|
||||||
["".join(["#" if x else "." for x in button]) for button in line[1]],
|
[
|
||||||
|
"".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], [[False] * len(line[0])]))
|
output.append(traverse_tree(line[0], line[1:-1], {"".join("." * len(line[0]))}))
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
input_filepath = "input/manual.txt"
|
input_filepath = "input/manual.txt"
|
||||||
input_manual = parse_input(input_filepath)
|
input_manual = parse_input(input_filepath)
|
||||||
# debug("MANUAL\n%s", "\n".join([str(x) for x in input_manual]))
|
debug("MANUAL\n%s", input_manual)
|
||||||
shortest_solutions = find_shortest_solutions(input_manual)
|
shortest_solutions = find_shortest_solutions(input_manual)
|
||||||
debug(f"SHORTEST SOLUTIONS: {shortest_solutions}")
|
debug(f"SHORTEST SOLUTIONS: {shortest_solutions}")
|
||||||
print(f"The sum of shortest solution is: {sum(shortest_solutions)}")
|
print(f"The sum of shortest solution is: {sum(shortest_solutions)}")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue