aboutsummaryrefslogtreecommitdiff
path: root/2021/12
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-12-12 06:46:33 +0100
committerThomas Voss <thomasvoss@live.com> 2021-12-12 06:46:33 +0100
commitc93fc1b9a11c96a4554ec47029bf085597fdef60 (patch)
treee7592b7b7c425646509b7676e9112af33d01ce57 /2021/12
parent96766ceb3509cf5d7c96ad5dae7f08d9de4984f7 (diff)
Fix defaultdict typehint and use default func args
Diffstat (limited to '2021/12')
-rwxr-xr-x2021/12/puzzle-1.py32
-rwxr-xr-x2021/12/puzzle-2.py37
-rw-r--r--2021/12/puzzles.py14
3 files changed, 76 insertions, 7 deletions
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()
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()
diff --git a/2021/12/puzzles.py b/2021/12/puzzles.py
index 8cfc506..2194c49 100644
--- a/2021/12/puzzles.py
+++ b/2021/12/puzzles.py
@@ -3,24 +3,24 @@
from collections import defaultdict
-def solve(paths: defaultdict[list[str]], path: str) -> int:
+def solve(paths: defaultdict[list[str]], path: str, flag: bool = False) -> int:
acc = 0
- tokens = path[0].split(",")
+ 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[0]},{dest}", False))
+ acc += solve(paths, f"{path},{dest}")
# END PART 1 START PART 2
elif dest != "start":
if dest.islower() and dest in tokens:
- if path[1]:
+ if flag:
continue
- acc += solve(paths, (f"{path[0]},{dest}", True))
+ acc += solve(paths, f"{path},{dest}", True)
else:
- acc += solve(paths, (f"{path[0]},{dest}", path[1]))
+ acc += solve(paths, f"{path},{dest}", flag)
# END PART 2
return acc
@@ -34,7 +34,7 @@ def main() -> None:
paths[x].append(y)
paths[y].append(x)
- print(solve(paths, ("start", False)))
+ print(solve(paths, "start", False))
if __name__ == "__main__":