diff --git a/albaer/day12/code.py b/albaer/day12/code.py new file mode 100644 index 0000000..44f572f --- /dev/null +++ b/albaer/day12/code.py @@ -0,0 +1,81 @@ +from pprint import pprint +from collections import Counter + +def write_solution(solution, output_file_path="solution.txt"): + with open(output_file_path, "w") as output_file: + output_file.write(str(solution)) + print(solution) + return solution + +def read_input_lines(input_file_path="input.txt"): + with open(input_file_path, "r") as input_file: + return input_file.read().splitlines() + +def split_inputs_from_first_line(): + input_lines = read_input_lines() + first_line_as_str = input_lines[0] + return first_line_as_str.split(",") + +# Utilities + +def flatten(lst): + return [item for sublist in lst for item in sublist] + +# Part 1 + +def other_cave(tunnel, cave): + return tunnel.replace(cave, "").replace("-", "") + +def caves_connected_to(tunnels, cave): + return [other_cave(tunnel, cave) for tunnel in tunnels if cave in tunnel] + +def add_cave_to_path_list(path_list, cave): + new_path_list = path_list.copy() + new_path_list.append(cave) + return new_path_list + +def paths_from(tunnels, path_list, valid): + last_cave = path_list[-1] + possible_next_caves = caves_connected_to(tunnels, last_cave) + valid_next_caves = [cave for cave in possible_next_caves if valid(path_list, cave)] + return [add_cave_to_path_list(path_list, cave) for cave in valid_next_caves] + +def valid_part_1(path_list, cave): + if cave == "start": + return False + elif cave.islower() and cave in path_list: + return False + else: + return True + +def valid_part_2(path_list, cave): + small_caves = [cave for cave in path_list if cave.islower()] + already_visited_small_twice = [k for k, v in Counter(small_caves).items() if v > 1] != [] + returning_to_small_cave = cave.islower() and cave in path_list + if cave == "start": + return False + elif already_visited_small_twice and returning_to_small_cave: + return False + else: + return True + +def find_paths(tunnels, valid, paths=[["start"]], complete_paths=[], ): + if paths == []: + return complete_paths + else: + all_paths = flatten([paths_from(tunnels, path, valid) for path in paths]) + new_complete_paths = complete_paths + [path for path in all_paths if path[-1] == "end"] + new_paths = [path for path in all_paths if path[-1] != "end"] + return find_paths(tunnels, valid, new_paths, new_complete_paths) + +def count_distinct_paths(tunnels, valid): + return len(find_paths(tunnels, valid)) + +# Write solution + +if __name__ == '__main__': + input_tunnels = read_input_lines() + part_1_result = count_distinct_paths(input_tunnels, valid_part_1) + part_2_result = count_distinct_paths(input_tunnels, valid_part_2) + solution = str(part_1_result) + "\n" + str(part_2_result) + write_solution(solution) diff --git a/albaer/day12/input.txt b/albaer/day12/input.txt new file mode 100644 index 0000000..eef77fd --- /dev/null +++ b/albaer/day12/input.txt @@ -0,0 +1,25 @@ +YW-end +DK-la +la-XG +end-gy +zq-ci +XG-gz +TF-la +xm-la +gy-gz +ci-start +YW-ci +TF-zq +ci-DK +la-TS +zq-YW +gz-YW +zq-gz +end-gz +ci-TF +DK-zq +gy-YW +start-DK +gz-DK +zq-la +start-TF diff --git a/albaer/day12/readme.md b/albaer/day12/readme.md new file mode 100644 index 0000000..2606a50 --- /dev/null +++ b/albaer/day12/readme.md @@ -0,0 +1,100 @@ +--- Day 12: Passage Pathing --- + +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? diff --git a/albaer/day12/solution.txt b/albaer/day12/solution.txt new file mode 100644 index 0000000..f0419b2 --- /dev/null +++ b/albaer/day12/solution.txt @@ -0,0 +1,2 @@ +4912 +150004 \ No newline at end of file diff --git a/albaer/day12/tests.py b/albaer/day12/tests.py new file mode 100644 index 0000000..fbfed81 --- /dev/null +++ b/albaer/day12/tests.py @@ -0,0 +1,85 @@ +import unittest +from code import * + +class TestDay12(unittest.TestCase): + EXAMPLE_INPUT_A = [ + "start-A", + "start-b", + "A-c", + "A-b", + "b-d", + "A-end", + "b-end", + ] + + EXAMPLE_INPUT_B = [ + "dc-end", + "HN-start", + "start-kj", + "dc-start", + "dc-HN", + "LN-dc", + "HN-end", + "kj-sa", + "kj-HN", + "kj-dc", + ] + + EXAMPLE_INPUT_C = [ + "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", + ] + + # Part 1 + + def test_count_pare_1_distinct_paths_a(self): + actual = count_distinct_paths(self.EXAMPLE_INPUT_A, valid_part_1) + expected = 10 + self.assertEqual(actual, expected) + + def test_count_pare_1_distinct_paths_b(self): + actual = count_distinct_paths(self.EXAMPLE_INPUT_B, valid_part_1) + expected = 19 + self.assertEqual(actual, expected) + + def test_count_pare_1_distinct_paths_c(self): + actual = count_distinct_paths(self.EXAMPLE_INPUT_C, valid_part_1) + expected = 226 + self.assertEqual(actual, expected) + + # Part 2 + + def test_count_part_2_distinct_paths_a(self): + actual = count_distinct_paths(self.EXAMPLE_INPUT_A, valid_part_2) + expected = 36 + self.assertEqual(actual, expected) + + def test_count_part_2_distinct_paths_b(self): + actual = count_distinct_paths(self.EXAMPLE_INPUT_B, valid_part_2) + expected = 103 + self.assertEqual(actual, expected) + + def test_count_part_2_distinct_paths_c(self): + actual = count_distinct_paths(self.EXAMPLE_INPUT_C, valid_part_2) + expected = 3509 + self.assertEqual(actual, expected) + +if __name__ == '__main__': + unittest.main() +