diff --git a/day12.py b/day12.py new file mode 100644 index 0000000..d418af9 --- /dev/null +++ b/day12.py @@ -0,0 +1,54 @@ +from tools.aoc import AOCDay +from typing import Any, Dict, List + + +def getCaveMap(lines: List[List[str]]) -> Dict[str, List[str]]: + m = {} + for l in lines: + if l[0] in m: + m[l[0]].append(l[1]) + else: + m[l[0]] = [l[1]] + + if l[1] in m: + m[l[1]].append(l[0]) + else: + m[l[1]] = [l[0]] + + return m + + +def getPaths(caveMap: Dict[str, List[str]], start: str, visited: List[str], noDouble: bool = True) -> List[List[str]]: + paths = [] + visited.append(start) + for thisCave in caveMap[start]: + if thisCave == 'start': + continue + + if thisCave == 'end': + paths.append(['end']) + continue + + foundDouble = False + if thisCave.islower() and thisCave in visited: + if noDouble: + continue + foundDouble = True + + sub_paths = getPaths(caveMap, thisCave, visited.copy(), noDouble or foundDouble) + [paths.append(x) for x in sub_paths if 'end' in x] + + return paths + + +class Day(AOCDay): + test_solutions_p1 = [10, 19, 226, 5076] + test_solutions_p2 = [36, 103, 3509, 145643] + + def part1(self) -> Any: + caveMap = getCaveMap(self.getInputAsArraySplit("-")) + return len(getPaths(caveMap, 'start', [])) + + def part2(self) -> Any: + caveMap = getCaveMap(self.getInputAsArraySplit("-")) + return len(getPaths(caveMap, 'start', [], False)) diff --git a/inputs/input12 b/inputs/input12 new file mode 100644 index 0000000..1296eb6 --- /dev/null +++ b/inputs/input12 @@ -0,0 +1,22 @@ +end-MY +MY-xc +ho-NF +start-ho +NF-xc +NF-yf +end-yf +xc-TP +MY-qo +yf-TP +dc-NF +dc-xc +start-dc +yf-MY +MY-ho +EM-uh +xc-yf +ho-dc +uh-NF +yf-ho +end-uh +start-NF diff --git a/inputs/test_input12_1_0 b/inputs/test_input12_1_0 new file mode 100644 index 0000000..6fd8c41 --- /dev/null +++ b/inputs/test_input12_1_0 @@ -0,0 +1,7 @@ +start-A +start-b +A-c +A-b +b-d +A-end +b-end diff --git a/inputs/test_input12_1_1 b/inputs/test_input12_1_1 new file mode 100644 index 0000000..62cc714 --- /dev/null +++ b/inputs/test_input12_1_1 @@ -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/inputs/test_input12_1_2 b/inputs/test_input12_1_2 new file mode 100644 index 0000000..65f3833 --- /dev/null +++ b/inputs/test_input12_1_2 @@ -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/inputs/test_input12_1_3 b/inputs/test_input12_1_3 new file mode 100644 index 0000000..1296eb6 --- /dev/null +++ b/inputs/test_input12_1_3 @@ -0,0 +1,22 @@ +end-MY +MY-xc +ho-NF +start-ho +NF-xc +NF-yf +end-yf +xc-TP +MY-qo +yf-TP +dc-NF +dc-xc +start-dc +yf-MY +MY-ho +EM-uh +xc-yf +ho-dc +uh-NF +yf-ho +end-uh +start-NF diff --git a/inputs/test_input12_2_0 b/inputs/test_input12_2_0 new file mode 100644 index 0000000..6fd8c41 --- /dev/null +++ b/inputs/test_input12_2_0 @@ -0,0 +1,7 @@ +start-A +start-b +A-c +A-b +b-d +A-end +b-end diff --git a/inputs/test_input12_2_1 b/inputs/test_input12_2_1 new file mode 100644 index 0000000..62cc714 --- /dev/null +++ b/inputs/test_input12_2_1 @@ -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/inputs/test_input12_2_2 b/inputs/test_input12_2_2 new file mode 100644 index 0000000..65f3833 --- /dev/null +++ b/inputs/test_input12_2_2 @@ -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/inputs/test_input12_2_3 b/inputs/test_input12_2_3 new file mode 100644 index 0000000..1296eb6 --- /dev/null +++ b/inputs/test_input12_2_3 @@ -0,0 +1,22 @@ +end-MY +MY-xc +ho-NF +start-ho +NF-xc +NF-yf +end-yf +xc-TP +MY-qo +yf-TP +dc-NF +dc-xc +start-dc +yf-MY +MY-ho +EM-uh +xc-yf +ho-dc +uh-NF +yf-ho +end-uh +start-NF