day09: slightly optimized
This commit is contained in:
parent
08f93c574a
commit
83385d81e6
16
day09.py
16
day09.py
@ -15,17 +15,9 @@ def getCaveMapFromInput(puzzle_input: List[str]) -> Grid:
|
||||
|
||||
def getLowPoints(caveMap: Grid) -> List[Coordinate]:
|
||||
lowPoints = []
|
||||
for x in range(caveMap.minX, caveMap.maxX + 1):
|
||||
for y in range(caveMap.minY, caveMap.maxY + 1):
|
||||
thisPoint = Coordinate(x, y)
|
||||
lowest = True
|
||||
|
||||
for t in thisPoint.getNeighbours(includeDiagonal=False):
|
||||
if caveMap.get(t) <= caveMap.get(Coordinate(x, y)):
|
||||
lowest = False
|
||||
|
||||
if lowest:
|
||||
lowPoints.append(thisPoint)
|
||||
for point in caveMap.getActiveCells():
|
||||
if not sum((caveMap.get(t) <= caveMap.get(point)) for t in point.getNeighbours(includeDiagonal=False)):
|
||||
lowPoints.append(point)
|
||||
|
||||
return lowPoints
|
||||
|
||||
@ -35,7 +27,7 @@ def getBasin(caveMap: Grid, start: Coordinate, visited: set) -> set:
|
||||
if n in visited:
|
||||
continue
|
||||
|
||||
if caveMap.get(n) != 9 and caveMap.get(n) != 99:
|
||||
if caveMap.get(n) < 9:
|
||||
visited.add(n)
|
||||
visited = set.union(visited, getBasin(caveMap, n, visited))
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user