From c93fc1b9a11c96a4554ec47029bf085597fdef60 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sun, 12 Dec 2021 06:46:33 +0100 Subject: Fix defaultdict typehint and use default func args --- 2021/12/puzzle-1.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100755 2021/12/puzzle-1.py (limited to '2021/12/puzzle-1.py') diff --git a/2021/12/puzzle-1.py b/2021/12/puzzle-1.py new file mode 100755 index 0000000..1ec9f51 --- /dev/null +++ b/2021/12/puzzle-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +from collections import defaultdict + + +def solve(paths: defaultdict[list[str]], path: str, flag: bool = False) -> int: + acc = 0 + tokens = path.split(",") + + for dest in paths[tokens[-1]]: + if dest == "end": + acc += 1 + # START PART 1 + elif not (dest.islower() and dest in tokens): + acc += solve(paths, f"{path},{dest}") + + return acc + + +def main() -> None: + paths: defaultdict[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() -- cgit v1.2.3