diff options
author | Thomas Voss <thomasvoss@live.com> | 2021-12-12 06:42:21 +0100 |
---|---|---|
committer | Thomas Voss <thomasvoss@live.com> | 2021-12-12 06:42:21 +0100 |
commit | 96766ceb3509cf5d7c96ad5dae7f08d9de4984f7 (patch) | |
tree | 9fb80f004059aecd935a814c1bf1bfe2b15866ad /2021 | |
parent | e11577db2b9af3451a727353bb9e928f614774b2 (diff) |
Add day 12 solutions
Diffstat (limited to '2021')
-rw-r--r-- | 2021/12/Makefile | 8 | ||||
-rw-r--r-- | 2021/12/input | 25 | ||||
-rw-r--r-- | 2021/12/puzzles.py | 41 |
3 files changed, 74 insertions, 0 deletions
diff --git a/2021/12/Makefile b/2021/12/Makefile new file mode 100644 index 0000000..247194a --- /dev/null +++ b/2021/12/Makefile @@ -0,0 +1,8 @@ +all: + sed '/START PART 2/,/END PART 2/d' puzzles.py >puzzle-1.py + sed '/START PART 1/,/END PART 1/d' puzzles.py >puzzle-2.py + chmod +x puzzle-[12].py + +.PHONY: clean +clean: + rm -f puzzle-[12].py diff --git a/2021/12/input b/2021/12/input new file mode 100644 index 0000000..1cacb23 --- /dev/null +++ b/2021/12/input @@ -0,0 +1,25 @@ +qi-UD +jt-br +wb-TF +VO-aa +UD-aa +br-end +end-HA +qi-br +br-HA +UD-start +TF-qi +br-hf +VO-hf +start-qi +end-aa +hf-HA +hf-UD +aa-hf +TF-hf +VO-start +wb-aa +UD-wb +KX-wb +qi-VO +br-TF diff --git a/2021/12/puzzles.py b/2021/12/puzzles.py new file mode 100644 index 0000000..8cfc506 --- /dev/null +++ b/2021/12/puzzles.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +from collections import defaultdict + + +def solve(paths: defaultdict[list[str]], path: str) -> int: + acc = 0 + tokens = path[0].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)) + # END PART 1 START PART 2 + elif dest != "start": + if dest.islower() and dest in tokens: + if path[1]: + continue + acc += solve(paths, (f"{path[0]},{dest}", True)) + else: + acc += solve(paths, (f"{path[0]},{dest}", path[1])) + # END PART 2 + + 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() |