def best_destination(positions: list[int], cost_fn: CostFunction = cost_linear): # TODO: optimize # convex function -> stop after it starts to grow # can be likely estimated with a quadratic function (for both cost_fns?) # part 1 -> probably median works? return min(range(*minmax(positions)), key=lambda d: alignment_cost(positions, d, cost_fn))
def draw_plan(self): def tile_at(p: Point) -> Tile: if p == self.pos: return Tile.ROBOT elif p == Point(0, 0): return Tile.ORIGIN elif p in self.plan: return self.plan[p] else: return Tile.UNKNOWN def char_at(cx: int, cy: int) -> str: return tile_at(Point(cx, cy)).char min_x, max_x = minmax(p.x for p in self.plan.keys()) min_y, max_y = minmax(p.y for p in self.plan.keys()) for y in range(min_y, max_y + 1): print(''.join(char_at(x, y) for x in range(min_x, max_x + 1)))
def draw(self): if not self.bugs: print("all dead :(") return def c(x: int, y: int, z: int) -> str: pos = (x, y, z) if (x, y) == self.center: assert pos not in self.bugs return '?' elif pos in self.bugs: return '#' else: return '.' min_z, max_z = minmax(z for x, y, z in self.bugs) for z in range(min_z, max_z + 1): print(f"Depth {z}:") for y in range(self.height): print(''.join(c(x, y, z) for x in range(self.width))) print()
def draw_grid(grid: Grid): min_x, max_x = minmax(pos.x for pos in grid.keys()) min_y, max_y = minmax(pos.y for pos in grid.keys()) c = {Color.BLACK: '.', Color.WHITE: '#', Color.NONE: ' '} for y in range(min_y, max_y + 1): print(''.join(c[grid[Point(x, y)]] for x in range(min_x, max_x + 1)))
def _xrange(self, margin: int = 3) -> range: xmin, xmax = minmax(self.alive) return range(xmin - margin, xmax + margin + 1)