diff --git a/2021/12/README.md b/2021/12/README.md new file mode 100644 index 0000000..6b2a53f --- /dev/null +++ b/2021/12/README.md @@ -0,0 +1,165 @@ +# 2021 Day 12: Passage Pathing +Copyright (c) Eric Wastl +#### [Direct Link](https://adventofcode.com/2021/day/12) + +## Part 1 + +With your submarine's subterranean subsystems subsisting suboptimally, the only way you're getting out of this cave anytime soon is by finding a path yourself. Not just **a** path - the only way to know if you've found the **best** path is to find **all** of them. + +Fortunately, the sensors are still mostly working, and so you build a rough map of the remaining caves (your puzzle input). For example: + +``` +start-A +start-b +A-c +A-b +b-d +A-end +b-end +``` + +This is a list of how all of the caves are connected. You start in the cave named `start`, and your destination is the cave named `end`. An entry like `b-d` means that cave `b` is connected to cave `d` - that is, you can move between them. + +So, the above cave system looks roughly like this: + +``` + start + / \ +c--A-----b--d + \ / + end +``` + +Your goal is to find the number of distinct **paths** that start at `start`, end at `end`, and don't visit small caves more than once. There are two types of caves: **big** caves (written in uppercase, like `A`) and **small** caves (written in lowercase, like `b`). It would be a waste of time to visit any small cave more than once, but big caves are large enough that it might be worth visiting them multiple times. So, all paths you find should **visit small caves at most once**, and can **visit big caves any number of times**. + +Given these rules, there are **`10`** paths through this example cave system: + +``` +start,A,b,A,c,A,end +start,A,b,A,end +start,A,b,end +start,A,c,A,b,A,end +start,A,c,A,b,end +start,A,c,A,end +start,A,end +start,b,A,c,A,end +start,b,A,end +start,b,end +``` + +(Each line in the above list corresponds to a single path; the caves visited by that path are listed in the order they are visited and separated by commas.) + +Note that in this cave system, cave `d` is never visited by any path: to do so, cave `b` would need to be visited twice (once on the way to cave `d` and `a` second time when returning from cave `d`), and since cave `b` is small, this is not allowed. + +Here is a slightly larger example: + +``` +dc-end +HN-start +start-kj +dc-start +dc-HN +LN-dc +HN-end +kj-sa +kj-HN +kj-dc +``` + +The `19` paths through it are as follows: + +``` +start,HN,dc,HN,end +start,HN,dc,HN,kj,HN,end +start,HN,dc,end +start,HN,dc,kj,HN,end +start,HN,end +start,HN,kj,HN,dc,HN,end +start,HN,kj,HN,dc,end +start,HN,kj,HN,end +start,HN,kj,dc,HN,end +start,HN,kj,dc,end +start,dc,HN,end +start,dc,HN,kj,HN,end +start,dc,end +start,dc,kj,HN,end +start,kj,HN,dc,HN,end +start,kj,HN,dc,end +start,kj,HN,end +start,kj,dc,HN,end +start,kj,dc,end +``` + +Finally, this even larger example has `226` paths through it: + +``` +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 +``` + +**How many paths through this cave system are there that visit small caves at most once?** + +## Part 2 + +After reviewing the available paths, you realize you might have time to visit a single small cave **twice**. Specifically, big caves can be visited any number of times, a single small cave can be visited at most twice, and the remaining small caves can be visited at most once. However, the caves named `start` and `end` can only be visited **exactly once each**: once you leave the `start` cave, you may not return to it, and once you reach the `end` cave, the path must end immediately. + +Now, the `36` possible paths through the first example above are: + +``` +start,A,b,A,b,A,c,A,end +start,A,b,A,b,A,end +start,A,b,A,b,end +start,A,b,A,c,A,b,A,end +start,A,b,A,c,A,b,end +start,A,b,A,c,A,c,A,end +start,A,b,A,c,A,end +start,A,b,A,end +start,A,b,d,b,A,c,A,end +start,A,b,d,b,A,end +start,A,b,d,b,end +start,A,b,end +start,A,c,A,b,A,b,A,end +start,A,c,A,b,A,b,end +start,A,c,A,b,A,c,A,end +start,A,c,A,b,A,end +start,A,c,A,b,d,b,A,end +start,A,c,A,b,d,b,end +start,A,c,A,b,end +start,A,c,A,c,A,b,A,end +start,A,c,A,c,A,b,end +start,A,c,A,c,A,end +start,A,c,A,end +start,A,end +start,b,A,b,A,c,A,end +start,b,A,b,A,end +start,b,A,b,end +start,b,A,c,A,b,A,end +start,b,A,c,A,b,end +start,b,A,c,A,c,A,end +start,b,A,c,A,end +start,b,A,end +start,b,d,b,A,c,A,end +start,b,d,b,A,end +start,b,d,b,end +start,b,end +``` + +The slightly larger example above now has `103` paths through it, and the even larger example now has `3509` paths through it. + +Given these new rules, **how many paths through this cave system are there?** diff --git a/2021/12/code.py b/2021/12/code.py new file mode 100644 index 0000000..65330c0 --- /dev/null +++ b/2021/12/code.py @@ -0,0 +1,57 @@ +# SPDX-License-Identifier: MIT +# Copyright (c) 2021 Akumatic +# +# https://adventofcode.com/2021/day/12 + +def add_to_dict(d: dict, key: str, value: str): + if key not in d: + d[key] = [value] + else: + d[key].append(value) + +def parse_paths(connections: list): + paths = dict() + for connection in connections: + tmp = connection.split("-") + add_to_dict(paths, tmp[0], tmp[1]) + add_to_dict(paths, tmp[1], tmp[0]) + return paths + +def read_file(filename: str = "input.txt") -> list: + with open(f"{__file__.rstrip('code.py')}{filename}", "r") as f: + return [line for line in f.read().strip().split("\n")] + +def part1(paths: dict) -> int: + complete = list() + incomplete = [["start"]] + while incomplete: + previous = incomplete.pop() + for next_step in paths[previous[-1]]: + if next_step.lower() not in previous: + p = previous[:] + p.append(next_step) + complete.append(p) if next_step == "end" else incomplete.append(p) + return len(complete) + +def part2(paths: dict) -> int: + complete = list() + incomplete = [["start"]] + small = [x for x in paths if x.islower() and x != "end" and x != "start"] + while incomplete: + previous = incomplete.pop() + for next_step in paths[previous[-1]]: + if next_step == "start": + continue + if next_step in small: + if next_step in previous and any(previous.count(x) > 1 for x in small): + continue + p = previous[:] + p.append(next_step) + complete.append(p) if next_step == "end" else incomplete.append(p) + return len(complete) + +if __name__ == "__main__": + vals = read_file() + paths = parse_paths(vals) + print(f"Part 1: {part1(paths)}") + print(f"Part 2: {part2(paths)}") diff --git a/2021/12/input.txt b/2021/12/input.txt new file mode 100644 index 0000000..1cacb23 --- /dev/null +++ b/2021/12/input.txt @@ -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/solution.txt b/2021/12/solution.txt new file mode 100644 index 0000000..4e4b704 --- /dev/null +++ b/2021/12/solution.txt @@ -0,0 +1,2 @@ +Part 1: 3856 +Part 2: 116692 \ No newline at end of file diff --git a/2021/12/test_code.py b/2021/12/test_code.py new file mode 100644 index 0000000..412fcde --- /dev/null +++ b/2021/12/test_code.py @@ -0,0 +1,23 @@ +# SPDX-License-Identifier: MIT +# Copyright (c) 2021 Akumatic + +from code import part1, part2, read_file, parse_paths + +def test(): + vals_1 = read_file("test_input_1.txt") + vals_2 = read_file("test_input_2.txt") + vals_3 = read_file("test_input_3.txt") + paths_1 = parse_paths(vals_1) + paths_2 = parse_paths(vals_2) + paths_3 = parse_paths(vals_3) + assert part1(paths_1) == 10 + assert part1(paths_2) == 19 + assert part1(paths_3) == 226 + print("Passed Part 1") + assert part2(paths_1) == 36 + assert part2(paths_2) == 103 + assert part2(paths_3) == 3509 + print("Passed Part 2") + +if __name__ == "__main__": + test() diff --git a/2021/12/test_input_1.txt b/2021/12/test_input_1.txt new file mode 100644 index 0000000..6fd8c41 --- /dev/null +++ b/2021/12/test_input_1.txt @@ -0,0 +1,7 @@ +start-A +start-b +A-c +A-b +b-d +A-end +b-end diff --git a/2021/12/test_input_2.txt b/2021/12/test_input_2.txt new file mode 100644 index 0000000..62cc714 --- /dev/null +++ b/2021/12/test_input_2.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/2021/12/test_input_3.txt b/2021/12/test_input_3.txt new file mode 100644 index 0000000..65f3833 --- /dev/null +++ b/2021/12/test_input_3.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 diff --git a/2021/README.md b/2021/README.md index 2bd914b..058a726 100644 --- a/2021/README.md +++ b/2021/README.md @@ -26,7 +26,7 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day | 09 | :white_check_mark: | :white_check_mark: | [Solution](09/code.py) | [Day 09](https://adventofcode.com/2021/day/9) | | 10 | :white_check_mark: | :white_check_mark: | [Solution](10/code.py) | [Day 10](https://adventofcode.com/2021/day/10) | | 11 | :white_check_mark: | :white_check_mark: | [Solution](11/code.py) | [Day 11](https://adventofcode.com/2021/day/11) | -| 12 | | | | | +| 12 | :white_check_mark: | :white_check_mark: | [Solution](12/code.py) | [Day 12](https://adventofcode.com/2021/day/12) | | 13 | | | | | | 14 | | | | | | 15 | | | | |