aoc2021/day12.py
2021-12-12 07:17:21 +01:00

49 lines
1.3 KiB
Python

from collections import defaultdict
from tools.aoc import AOCDay
from typing import Any, Dict, List
def getCaveMap(lines: List[List[str]]) -> Dict[str, List[str]]:
m = defaultdict(list)
for l, r in lines:
m[l].append(r)
m[r].append(l)
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))