From 9a327ec92a8d8931f200783f2f1b58b3271a9b81 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Sun, 12 Dec 2021 07:41:40 +0100 Subject: [PATCH] day12: go faster --- day12.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/day12.py b/day12.py index 52b5be1..6713bc9 100644 --- a/day12.py +++ b/day12.py @@ -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))