day12: go faster
This commit is contained in:
parent
86745e1d2c
commit
9a327ec92a
12
day12.py
12
day12.py
@ -12,9 +12,9 @@ def getCaveMap(lines: List[List[str]]) -> Dict[str, List[str]]:
|
|||||||
return m
|
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 = []
|
paths = []
|
||||||
visited.append(start)
|
visited.add(start)
|
||||||
for thisCave in caveMap[start]:
|
for thisCave in caveMap[start]:
|
||||||
if thisCave == 'start':
|
if thisCave == 'start':
|
||||||
continue
|
continue
|
||||||
@ -30,7 +30,9 @@ def getPaths(caveMap: Dict[str, List[str]], start: str, visited: List[str], noDo
|
|||||||
foundDouble = True
|
foundDouble = True
|
||||||
|
|
||||||
sub_paths = getPaths(caveMap, thisCave, visited.copy(), noDouble or foundDouble)
|
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
|
return paths
|
||||||
|
|
||||||
@ -41,8 +43,8 @@ class Day(AOCDay):
|
|||||||
|
|
||||||
def part1(self) -> Any:
|
def part1(self) -> Any:
|
||||||
caveMap = getCaveMap(self.getInputAsArraySplit("-"))
|
caveMap = getCaveMap(self.getInputAsArraySplit("-"))
|
||||||
return len(getPaths(caveMap, 'start', []))
|
return len(getPaths(caveMap, 'start', set()))
|
||||||
|
|
||||||
def part2(self) -> Any:
|
def part2(self) -> Any:
|
||||||
caveMap = getCaveMap(self.getInputAsArraySplit("-"))
|
caveMap = getCaveMap(self.getInputAsArraySplit("-"))
|
||||||
return len(getPaths(caveMap, 'start', [], False))
|
return len(getPaths(caveMap, 'start', set(), False))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user