58 lines
1.3 KiB
Python
Executable file
58 lines
1.3 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
from logging import debug, DEBUG, basicConfig
|
|
from sys import argv
|
|
|
|
|
|
io: dict[str, list[str]] = {}
|
|
paths: list[list[str]] = []
|
|
|
|
|
|
def parse_input(input_filepath: str) -> dict[str, 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")
|
|
|
|
input_dict = {}
|
|
for line in input_data:
|
|
input_dict[line.strip().split(":")[0]] = line.strip().split(" ")[1:]
|
|
|
|
return input_dict
|
|
|
|
|
|
def traverse_path(path: list[str]):
|
|
global paths
|
|
if path[len(path) - 1] == "out":
|
|
paths.append(path)
|
|
return
|
|
elif path[len(path) - 1] in path[:-1]:
|
|
return
|
|
for p in io[path[len(path) - 1]]:
|
|
traverse_path(path + [p])
|
|
|
|
|
|
def find_paths():
|
|
global paths
|
|
path = ["you"]
|
|
traverse_path(path)
|
|
|
|
|
|
def main() -> None:
|
|
global io
|
|
global paths
|
|
input_filepath = "input/server_rack.txt"
|
|
io = parse_input(input_filepath)
|
|
debug(f"INPUT DICT: {io}")
|
|
find_paths()
|
|
debug(f"PATHS FOUND: {paths}")
|
|
print(f'There are {len(paths)} paths from "you" to "out"')
|
|
return
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if "-d" in argv or "--debug" in argv:
|
|
basicConfig(filename="debug.log", level=DEBUG)
|
|
main()
|
|
exit(0)
|