def part2(self): waypoint = Coordinate(10, 1) ship = Coordinate(0, 0) for line in self.lines: value = int(line[1:]) command = line[0] if command == "N": waypoint = waypoint.up(value) elif command == "S": waypoint = waypoint.down(value) elif command == "E": waypoint = waypoint.right(value) elif command == "W": waypoint = waypoint.left(value) elif command == "L": waypoint = waypoint.ccw_around(ship, value // 90) elif command == "R": waypoint = waypoint.cw_around(ship, value // 90) elif command == "F": diff_coordinate = waypoint - ship ship = ship + (diff_coordinate * value) waypoint = ship + diff_coordinate result = ship.manhattan(Coordinate(0, 0)) print("Part 2:", result)
def __init__(self, file_name): lines = Input(file_name).lines() self.coordinates: Set[Coordinate] = set() self.grid = InfiniteGrid[Coordinate]() for line in lines: x, y = line.split(', ') coordinate = Coordinate(int(x), int(y), system=CoordinateSystem.X_RIGHT_Y_DOWN) self.coordinates.add(coordinate) self.grid[coordinate] = coordinate self.bounding_box: BoundingBox = self.grid.bounding_box for x, y in self.bounding_box: test_coordinate = Coordinate( x, y, system=CoordinateSystem.X_RIGHT_Y_DOWN) distances = dict( (coordinate, test_coordinate.manhattan(coordinate)) for coordinate in self.coordinates) _, min_distance = min(distances.items(), key=lambda i: i[1]) min_coordinates = [ coordinate for coordinate, distance in distances.items() if distance == min_distance ] if len(min_coordinates) != 1: continue self.grid[test_coordinate] = min_coordinates[0]
def part2(self): result = 0 for x, y in self.bounding_box: coordinate = Coordinate(x, y, system=CoordinateSystem.X_RIGHT_Y_DOWN) total_distance = sum( coordinate.manhattan(i) for i in self.coordinates) if total_distance < 10000: result += 1 print("Part 2:", result)
def __call__(self, start: Coordinate, end: Coordinate) -> int: return start.manhattan(end)