From ab8817b8686e93dfda076fd8720dfa810ff733ad Mon Sep 17 00:00:00 2001 From: Zehvogel Date: Sun, 12 Dec 2021 12:48:03 +0100 Subject: [PATCH] day12 --- day12/input.txt | 22 +++++++++++++++++ day12/solution.py | 57 +++++++++++++++++++++++++++++++++++++++++++ day12/test_input.txt | 7 ++++++ day12/test_input2.txt | 10 ++++++++ day12/test_input3.txt | 18 ++++++++++++++ 5 files changed, 114 insertions(+) create mode 100644 day12/input.txt create mode 100644 day12/solution.py create mode 100644 day12/test_input.txt create mode 100644 day12/test_input2.txt create mode 100644 day12/test_input3.txt diff --git a/day12/input.txt b/day12/input.txt new file mode 100644 index 0000000..2159d08 --- /dev/null +++ b/day12/input.txt @@ -0,0 +1,22 @@ +um-end +pk-um +FE-il +ay-FE +pk-start +end-jt +um-FE +RO-il +xc-ay +il-end +start-EZ +pk-FE +xc-start +jt-FE +EZ-um +pk-xc +xc-EZ +pk-ay +il-ay +jt-EZ +jt-om +pk-EZ diff --git a/day12/solution.py b/day12/solution.py new file mode 100644 index 0000000..50dd67c --- /dev/null +++ b/day12/solution.py @@ -0,0 +1,57 @@ +import time + +def prepare_graph(input): + adjacencies = {} + lines = (l.strip() for l in input) + for l in lines: + a, b = l.split("-") + if a in adjacencies.keys(): + adjacencies[a].append(b) + else: + adjacencies[a] = [b] + if b in adjacencies.keys(): + adjacencies[b].append(a) + else: + adjacencies[b] = [a] + return adjacencies + +def visit(node, visited, adj, twice): + paths = [] + visited.append(node) + if node == "end": + paths.append(visited) + else: + for n in adj[node]: + if n.islower() and n in visited: + if n != "start" and not twice: + paths += visit(n, visited.copy(), adj, True) + else: + continue + else: + paths += visit(n, visited.copy(), adj, twice) + return paths + + +def part1(input): + adj = prepare_graph(input) + paths = visit("start", [], adj, True) + return len(paths) + +def part2(input): + adj = prepare_graph(input) + paths = visit("start", [], adj, False) + return len(paths) + +def main(): + with open("day12/input.txt") as f: + input = f.readlines() + t0 = time.perf_counter_ns() + print(f"solution for part 1 is: {part1(input)}") + t1 = time.perf_counter_ns() + print(f"part 1 took {t1-t0} ns") + print(f"solution for part 2 is: {part2(input)}") + t2 = time.perf_counter_ns() + print(f"part 2 took {t2-t1} ns") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/day12/test_input.txt b/day12/test_input.txt new file mode 100644 index 0000000..6fd8c41 --- /dev/null +++ b/day12/test_input.txt @@ -0,0 +1,7 @@ +start-A +start-b +A-c +A-b +b-d +A-end +b-end diff --git a/day12/test_input2.txt b/day12/test_input2.txt new file mode 100644 index 0000000..62cc714 --- /dev/null +++ b/day12/test_input2.txt @@ -0,0 +1,10 @@ +dc-end +HN-start +start-kj +dc-start +dc-HN +LN-dc +HN-end +kj-sa +kj-HN +kj-dc diff --git a/day12/test_input3.txt b/day12/test_input3.txt new file mode 100644 index 0000000..65f3833 --- /dev/null +++ b/day12/test_input3.txt @@ -0,0 +1,18 @@ +fs-end +he-DX +fs-he +start-DX +pj-DX +end-zg +zg-sl +zg-pj +pj-he +RW-he +fs-DX +pj-RW +zg-RW +start-pj +he-WI +zg-he +pj-fs +start-RW