diff options
Diffstat (limited to '2021/12/puzzle-2.py')
-rwxr-xr-x | 2021/12/puzzle-2.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/2021/12/puzzle-2.py b/2021/12/puzzle-2.py new file mode 100755 index 0000000..d5c6f68 --- /dev/null +++ b/2021/12/puzzle-2.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +from collections import defaultdict + + +def solve(paths: defaultdict[str, list[str]], path: str, flag: bool = False) -> int: + acc = 0 + tokens = path.split(",") + + for dest in paths[tokens[-1]]: + if dest == "end": + acc += 1 + elif dest != "start": + if dest.islower() and dest in tokens: + if flag: + continue + acc += solve(paths, f"{path},{dest}", True) + else: + acc += solve(paths, f"{path},{dest}", flag) + # END PART 2 + + return acc + + +def main() -> None: + paths: defaultdict[str, list[str]] = defaultdict(list) + with open("input", "r", encoding="utf-8") as f: + for entry in f.readlines(): + x, y = entry.strip().split("-") + paths[x].append(y) + paths[y].append(x) + + print(solve(paths, "start", False)) + + +if __name__ == "__main__": + main() |