From 1d06b5bdb5862a13510e2683059c876839c02449 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 12 Dec 2021 15:12:36 +0100 Subject: [PATCH 1/4] 2021: d12: ex1: add input --- 2021/d12/ex1/input | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 2021/d12/ex1/input diff --git a/2021/d12/ex1/input b/2021/d12/ex1/input new file mode 100644 index 0000000..c2b0b8d --- /dev/null +++ b/2021/d12/ex1/input @@ -0,0 +1,22 @@ +ax-end +xq-GF +end-xq +im-wg +ax-ie +start-ws +ie-ws +CV-start +ng-wg +ng-ie +GF-ng +ng-av +CV-end +ie-GF +CV-ie +im-xq +start-GF +GF-ws +wg-LY +CV-ws +im-CV +CV-wg From 4e46f615cf078f2204f607f451a880a1d7cfc75c Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 12 Dec 2021 15:13:10 +0100 Subject: [PATCH 2/4] 2021: d12: ex1: add solution --- 2021/d12/ex1/ex1.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 2021/d12/ex1/ex1.py diff --git a/2021/d12/ex1/ex1.py b/2021/d12/ex1/ex1.py new file mode 100755 index 0000000..4d0cacf --- /dev/null +++ b/2021/d12/ex1/ex1.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python + +import itertools +import sys +from collections import defaultdict +from typing import Dict, List, Set + +Map = Dict[str, Set[str]] + + +def solve(input: List[str]) -> int: + def parse() -> Map: + res: Map = defaultdict(set) + + for (start, to) in map(lambda s: s.split("-"), input): + res[start].add(to) + res[to].add(start) + + return res + + caves = parse() + + def dfs(start: str, seen: Set[str] = set()) -> int: + if start == "end": + return 1 + + seen = seen | {start} + res = 0 + + for dest in caves[start]: + if dest in seen and dest.islower(): + continue + res += dfs(dest, seen) + + return res + + return dfs("start") + + +def main() -> None: + input = [line.strip() for line in sys.stdin.readlines()] + print(solve(input)) + + +if __name__ == "__main__": + main() From 50ee5d1a50ed3c8a9fc35a2aeefa6777337d334e Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 12 Dec 2021 15:13:21 +0100 Subject: [PATCH 3/4] 2021: d12: ex2: add input --- 2021/d12/ex2/input | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 2021/d12/ex2/input diff --git a/2021/d12/ex2/input b/2021/d12/ex2/input new file mode 100644 index 0000000..c2b0b8d --- /dev/null +++ b/2021/d12/ex2/input @@ -0,0 +1,22 @@ +ax-end +xq-GF +end-xq +im-wg +ax-ie +start-ws +ie-ws +CV-start +ng-wg +ng-ie +GF-ng +ng-av +CV-end +ie-GF +CV-ie +im-xq +start-GF +GF-ws +wg-LY +CV-ws +im-CV +CV-wg From 8e030c9d88d5b2ec9e4f4500f05942b85da5a65c Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 12 Dec 2021 15:13:41 +0100 Subject: [PATCH 4/4] 2021: d12: ex2: add solution --- 2021/d12/ex2/ex2.py | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 2021/d12/ex2/ex2.py diff --git a/2021/d12/ex2/ex2.py b/2021/d12/ex2/ex2.py new file mode 100755 index 0000000..69d9aa9 --- /dev/null +++ b/2021/d12/ex2/ex2.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python + +import itertools +import sys +from collections import defaultdict +from typing import Dict, List, Set + +Map = Dict[str, Set[str]] + + +def solve(input: List[str]) -> int: + def parse() -> Map: + res: Map = defaultdict(set) + + for (start, to) in map(lambda s: s.split("-"), input): + res[start].add(to) + res[to].add(start) + + return res + + caves = parse() + + def dfs(start: str, seen: Set[str] = set(), has_doubled_back: bool = False) -> int: + if start == "end": + return 1 + + seen = seen | {start} + res = 0 + + for dest in caves[start]: + doubles_back = False + if dest in seen and dest.islower(): + if has_doubled_back or dest == "start": + continue + doubles_back = True + res += dfs(dest, seen, has_doubled_back or doubles_back) + + return res + + return dfs("start") + + +def main() -> None: + input = [line.strip() for line in sys.stdin.readlines()] + print(solve(input)) + + +if __name__ == "__main__": + main()