day12: go faster

This commit is contained in:
Stefan Harmuth 2021-12-12 07:41:40 +01:00
parent 86745e1d2c
commit 9a327ec92a

View File

@ -12,9 +12,9 @@ def getCaveMap(lines: List[List[str]]) -> Dict[str, List[str]]:
return m
def getPaths(caveMap: Dict[str, List[str]], start: str, visited: List[str], noDouble: bool = True) -> List[List[str]]:
def getPaths(caveMap: Dict[str, List[str]], start: str, visited: set, noDouble: bool = True) -> List[List[str]]:
paths = []
visited.append(start)
visited.add(start)
for thisCave in caveMap[start]:
if thisCave == 'start':
continue
@ -30,7 +30,9 @@ def getPaths(caveMap: Dict[str, List[str]], start: str, visited: List[str], noDo
foundDouble = True
sub_paths = getPaths(caveMap, thisCave, visited.copy(), noDouble or foundDouble)
[paths.append(x) for x in sub_paths if 'end' in x]
for x in sub_paths:
if 'end' in x:
paths.append(x)
return paths
@ -41,8 +43,8 @@ class Day(AOCDay):
def part1(self) -> Any:
caveMap = getCaveMap(self.getInputAsArraySplit("-"))
return len(getPaths(caveMap, 'start', []))
return len(getPaths(caveMap, 'start', set()))
def part2(self) -> Any:
caveMap = getCaveMap(self.getInputAsArraySplit("-"))
return len(getPaths(caveMap, 'start', [], False))
return len(getPaths(caveMap, 'start', set(), False))