def getPoints(point: com.Point, grid: com.CartesianGrid, gridLevel: int, direction): global grids check = grid.getPoint(*(point + direction)) if check is None: outside = getGridAtLevel(gridLevel - 1) bottom = [2, 1] top = [2, 3] leftSide = [1, 2] rightSide = [3, 2] pointToGet = {} pointToGet[tuple(grid.UP)] = top pointToGet[tuple(grid.DOWN)] = bottom pointToGet[tuple(grid.LEFT)] = leftSide pointToGet[tuple(grid.RIGHT)] = rightSide return [outside.getPoint(*pointToGet[tuple(direction)])] elif check.x == 2 and check.y == 2: inside = getGridAtLevel(gridLevel + 1) bottom = [[x, 0] for x in range(5)] top = [[x, 4] for x in range(5)] leftSide = [[0, y] for y in range(5)] rightSide = [[4, y] for y in range(5)] sliceToGet = {} sliceToGet[tuple(grid.UP)] = bottom sliceToGet[tuple(grid.DOWN)] = top sliceToGet[tuple(grid.LEFT)] = rightSide sliceToGet[tuple(grid.RIGHT)] = leftSide return [inside.getPoint(*sidePoint) for sidePoint in sliceToGet[tuple(direction)]] else: return [check]
def getOldAndNewSeatPart2(grid: com.CartesianGrid, x, y): point: com.Point = grid.getPoint(x, y) if point.data == 'L' and getOccupiedSeatCountPart2(grid, point) == 0: return point.data, '#' elif point.data == '#' and getOccupiedSeatCountPart2(grid, point) >= 5: return point.data, 'L' return point.data, point.data
def processFlash(grid: com.CartesianGrid, point: Point, alreadyFlashed: Set[Point]): if point in alreadyFlashed: return alreadyFlashed.add(point) for adjacentPoint in grid.getAdjacentPoints(point.x, point.y, True): adjacentPoint.data += 1 if adjacentPoint.data > 9: processFlash(grid, adjacentPoint, alreadyFlashed)
def getOccupiedSeatCountPart1(grid: com.CartesianGrid, point: com.Point): count = 0 for x in range(point.x - 1, point.x + 2): for y in range(point.y - 1, point.y + 2): if point.x == x and point.y == y: continue adjPoint = grid.getPoint(x, y) if adjPoint is not None and adjPoint.data == '#': count += 1 return count
def enhance(picture: com.CartesianGrid, algorithm: str, outsideValue: str): newPicture = com.CartesianGrid(flipOutput=True) for point in picture.getAllPoints(True): lookup = '' pointsToCheck = picture.getAdjacentPoints(point.x, point.y, True, True) pointsToCheck.append(point) pointsToCheck = sorted(pointsToCheck, key=lambda x: (x.y, x.x)) if len(pointsToCheck) != 9: test = 0 for x in pointsToCheck: data = x.data if data is None: data = outsideValue if data == '#': lookup += '1' else: lookup += '0' newValue = algorithm[int(lookup, 2)] newPicture.addPoint(com.Point(point.x, point.y, newValue)) return newPicture
def findBasin(grid: com.CartesianGrid, point: com.Point, basin: Set[com.Point]): if point.data == 9 or point in basin: return basin.add(point) adjacents = grid.getAdjacentPoints(point.x, point.y) for adjacent in adjacents: findBasin(grid, adjacent, basin) return
def newValue(point: com.Point, grid: com.CartesianGrid): directions = [grid.UP, grid.DOWN, grid.LEFT, grid.RIGHT] adjacent = 0 for direction in directions: check = grid.getPoint(*(point + direction)) if check is not None and check.data == '#': adjacent += 1 if point.data == '#': if adjacent != 1: return '.' return '#' else: if adjacent == 1 or adjacent == 2: return '#' return '.'
def getOccupiedSeatCountPart2(grid: com.CartesianGrid, point: com.Point): count = 0 upLeft = [-1, 1] upRight = [1, 1] downLeft = [-1, -1] downRight = [1, -1] directions = (upLeft, com.CartesianGrid.UP, upRight, com.CartesianGrid.RIGHT, downRight, com.CartesianGrid.DOWN, downLeft, com.CartesianGrid.LEFT) for direction in directions: adjPoint = point while adjPoint is not None: adjPoint = grid.getPoint(*(adjPoint + direction)) if adjPoint is None: break if adjPoint.data == '#': count += 1 break if adjPoint.data == 'L': break return count
def connectedInDirection(grid: com.CartesianGrid, point: com.Point, direction, steps: int, previouslyHit: set): pointInDirection = grid.getPoint(*(point + direction)) if pointInDirection is None or pointInDirection in previouslyHit: return [] previouslyHit.add(pointInDirection) return [(pointInDirection.data, steps + 1)]