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