def Part2(lines): grid = CartesianGrid('.', flipOutput=True) for line in lines: first, second = line.strip().split('->') firstX, firstY = first.strip().split(',') secondX, secondY = second.strip().split(',') firstX = int(firstX) firstY = int(firstY) secondX = int(secondX) secondY = int(secondY) if firstX == secondX: xValues = [firstX for x in range(1000)] elif firstX < secondX: xValues = range(firstX, secondX + 1, 1) else: xValues = range(firstX, secondX - 1, -1) if firstY == secondY: yValues = [firstY for x in range(1000)] elif firstY < secondY: yValues = range(firstY, secondY + 1, 1) else: yValues = range(firstY, secondY - 1, -1) for x, y in zip(xValues, yValues): markPoint(grid, x, y) return len([x for x in grid.getAllPoints() if x.data > 1]) #grid.__str__() #
def foldGrid(grid: CartesianGrid, axis, line): pointsToRemove = list() for point in grid.getAllPoints(True): if axis == 'y' and point.y > line: # fold up by finding the distance from the line and changing the y coord to an # equal distance above the line diff = point.y - line if grid.getPoint(point.x, line - diff): # remove this point because another one already exists pointsToRemove.append(point) else: grid.movePointTo(point, point.x, line - diff) if axis == 'x' and point.x > line: # fold left by finding the distance from the line and changing the x coord to an # equal distance left of the line diff = point.x - line if grid.getPoint(line - diff, point.y): # remove this point because another one already exists pointsToRemove.append(point) else: grid.movePointTo(point, line - diff, point.y) for point in pointsToRemove: grid.removePoint(point)
def Part1(lines: List[str]): grid = CartesianGrid(flipOutput=True) parse_to_grid(lines, grid, lambda x: int(x)) graph = Graph.parse_from_grid(grid, edgeWeightFinder= edgeWeightFcn) allPoints = grid.getAllPoints(True) start = allPoints[0] end = allPoints[-1] path, distance = graph.dijsktra(start, end) return distance
def parse_from_grid(grid: CartesianGrid, directed=True, edgeWeightFinder=None, includeDiagonals=False): graph = Graph(directed) for point in grid.getAllPoints(): graph.add_node(point) for adjacent in grid.getAdjacentPoints(point.x, point.y, includeDiagonals): graph.add_node(adjacent) edgeWeight = 1 if edgeWeightFinder: edgeWeight = edgeWeightFinder(point, adjacent) graph.add_edge(point, adjacent, edgeWeight, not (directed)) return graph
def Part1(lines): grid = CartesianGrid('0') for line in lines: first, second = line.strip().split('->') firstX, firstY = first.strip().split(',') secondX, secondY = second.strip().split(',') firstX = int(firstX) firstY = int(firstY) secondX = int(secondX) secondY = int(secondY) if not (firstX == secondX or firstY == secondY): continue if firstX == secondX: x = firstX for y in range(min(firstY, secondY), max(firstY, secondY) + 1): markPoint(grid, x, y) if firstY == secondY: y = firstY for x in range(min(firstX, secondX), max(firstX, secondX) + 1): markPoint(grid, x, y) return len([x for x in grid.getAllPoints() if x.data > 1])
def dupeGrid(grid: CartesianGrid): currentPoints = grid.getAllPoints(True) maxX = max([point.x for point in currentPoints]) + 1 maxY = max([point.x for point in currentPoints]) + 1 for xDupe in range(1,5): for point in currentPoints: newData = point.data + xDupe if newData >= 10: newData = (newData % 10) + 1 newPoint = Point((xDupe * maxX) + point.x, point.y, newData) grid.addPoint(newPoint) currentPoints = grid.getAllPoints(True) for yDupe in range(1,5): for point in currentPoints: newData = point.data + yDupe if newData >= 10: newData = (newData % 10) + 1 newPoint = Point(point.x, (yDupe * maxY) + point.y, newData) grid.addPoint(newPoint)