Exemplo n.º 1
0
 def __str__(self) -> "str":
     """
     >>> print(Region2DSet())
     >>> print(Region2DSet([Region2D(Point2D(0, 0), Point2D(2, 2))]))
     (0, 0)
     ###
     ###
     ###
     """
     if not self.regions:
         return ""
     min_coordinates, _ = min_and_max_tuples(region.min
                                             for region in self.regions)
     min_point = Point2D(min_coordinates)
     _, max_coordinates = min_and_max_tuples(region.max
                                             for region in self.regions)
     max_point = Point2D(max_coordinates)
     counts = {}
     for point in self:
         counts.setdefault(point, 0)
         counts[point] += 1
     return "{min_point}\n{as_string}".format(
         min_point=tuple(min_point),
         as_string="\n".join("".join(
             "#" if count == 1 else str(count) if count > 1 else "."
             for x in range(min_point.x, max_point.x + 1)
             for point in [Point2D(x, y)]
             for count in [counts.get(point, 0)])
                             for y in range(min_point.y, max_point.y + 1)),
     )
Exemplo n.º 2
0
    def turn_corners_on(self):
        (min_x, min_y), (max_x, max_y) = min_and_max_tuples(self.grid)
        for x in (min_x, max_x):
            for y in (min_y, max_y):
                self.grid[Point2D(x, y)] = True

        return self
 def get_bounding_box(self) -> Tuple[Tuple[int, int], Tuple[int, int]]:
     """
     >>> Sheet.from_sheet_text('''
     ...     6,10
     ...     0,14
     ...     9,10
     ...     0,3
     ...     10,4
     ...     4,11
     ...     6,0
     ...     6,12
     ...     4,1
     ...     0,13
     ...     10,12
     ...     3,4
     ...     3,0
     ...     8,4
     ...     1,10
     ...     2,14
     ...     8,10
     ...     9,0
     ... ''').get_bounding_box()
     ((0, 10), (0, 14))
     """
     (min_x, min_y), (max_x, max_y) = min_and_max_tuples(self.dots)
     return (min_x, max_x), (min_y, max_y)
Exemplo n.º 4
0
 def show(self, target: Point2D = Point2D.ZERO_POINT) -> str:
     """
     >>> print(NodeSetExtended.from_nodes_text(
     ...     "root@ebhq-gridcenter# df -h\\n"
     ...     "Filesystem            Size  Used  Avail  Use%\\n"
     ...     "/dev/grid/node-x0-y0   10T    8T     2T   80%\\n"
     ...     "/dev/grid/node-x0-y1   11T    6T     5T   54%\\n"
     ...     "/dev/grid/node-x0-y2   32T   28T     4T   87%\\n"
     ...     "/dev/grid/node-x1-y0    9T    7T     2T   77%\\n"
     ...     "/dev/grid/node-x1-y1    8T    0T     8T    0%\\n"
     ...     "/dev/grid/node-x1-y2   11T    7T     4T   63%\\n"
     ...     "/dev/grid/node-x2-y0   10T    6T     4T   60%\\n"
     ...     "/dev/grid/node-x2-y1    9T    8T     1T   88%\\n"
     ...     "/dev/grid/node-x2-y2    9T    6T     3T   66%\\n"
     ... ).show())
     (.) .  G
      .  _  .
      #  .  .
     """
     (min_x, min_y), (max_x, max_y) = min_and_max_tuples(self.nodes)
     return "\n".join(
         "".join(
             (
                 "({})"
                 if position == target else
                 " {} "
             ).format(
                 (
                     "_"
                     if node.used == 0 else
                     "G"
                     if position == self.position else
                     "."
                     if self.can_move_from_node_ever(node) else
                     "#"
                 )
                 if node else
                 " "
             )
             for position, node in (
                 (position, self.nodes.get(position))
                 for position in (
                     Point2D(x, y)
                     for x in range(min_x, max_x + 1)
                 )
             )
         ).rstrip()
         for y in range(min_y, max_y + 1)
     )
 def __str__(self) -> str:
     max_distance = max(self.distances.values()) or 1
     state_positions = {state.position for state in self.stack}
     (min_x, min_y), (max_x, max_y) = min_and_max_tuples(
         chain(
             self.distances,
             state_positions,
         ))
     return "\n".join(
         "".join("*" if point in state_positions else
                 str(int(9 * self.distances[point] /
                         max_distance)) if point in self.distances else "."
                 for x in range(min_x, max_x + 1)
                 for point in [Point2D(x, y)])
         for y in range(min_y, max_y + 1))
    def get_highest_y_for_initial_y_that_lands_in_area_y(
        self, initial_y: int,
    ) -> Optional[int]:
        if not (self.area.min.x == self.area.max.x == 0):
            return self\
                .with_zero_x_area()\
                .get_highest_y_for_initial_y_that_lands_in_area_y(initial_y)

        path = self.generate_path(
            Point2D.get_zero_point(),
            Point2D(0, initial_y),
        )
        last_point = path[-1]
        if last_point not in self.area:
            return None
        _, (_, max_y) = min_and_max_tuples(path)
        return max_y
 def __str__(self) -> str:
     """
     >>> print(":", Launch(
     ...     area=Area.from_area_text("target area: x=20..30, y=-10..-5"),
     ...     path=[
     ...         Point2D(0, 0), Point2D(7, 2), Point2D(13, 3),
     ...         Point2D(18, 3), Point2D(22, 2), Point2D(25, 0),
     ...         Point2D(27, -3), Point2D(28, -7),
     ...     ],
     ... ))
     : .............#....#............
     .......#..............#........
     ...............................
     S........................#.....
     ...............................
     ...............................
     ...........................#...
     ...............................
     ....................TTTTTTTTTTT
     ....................TTTTTTTTTTT
     ....................TTTTTTTT#TT
     ....................TTTTTTTTTTT
     ....................TTTTTTTTTTT
     ....................TTTTTTTTTTT
     """
     (min_x, min_y), (max_x, max_y) = \
         min_and_max_tuples(self.path + list(self.area))
     if self.path:
         start = self.path[0]
     else:
         start = None
     return "\n".join(
         "".join(
             "S"
             if point == start else
             "#"
             if point in self.path else
             "T"
             if point in self.area else
             "."
             for x in range(min_x, max_x + 1)
             for point in [Point2D(x, y)]
         )
         for y in range(max_y, min_y - 1, -1)
     )
Exemplo n.º 8
0
 def show_capacities(self, target: Point2D = Point2D.ZERO_POINT) -> str:
     """
     >>> print(NodeSetExtended.from_nodes_text(
     ...     "root@ebhq-gridcenter# df -h\\n"
     ...     "Filesystem            Size  Used  Avail  Use%\\n"
     ...     "/dev/grid/node-x0-y0   10T    8T     2T   80%\\n"
     ...     "/dev/grid/node-x0-y1   11T    6T     5T   54%\\n"
     ...     "/dev/grid/node-x0-y2   32T   28T     4T   87%\\n"
     ...     "/dev/grid/node-x1-y0    9T    7T     2T   77%\\n"
     ...     "/dev/grid/node-x1-y1    8T    0T     8T    0%\\n"
     ...     "/dev/grid/node-x1-y2   11T    7T     4T   63%\\n"
     ...     "/dev/grid/node-x2-y0   10T    6T     4T   60%\\n"
     ...     "/dev/grid/node-x2-y1    9T    8T     1T   88%\\n"
     ...     "/dev/grid/node-x2-y2    9T    6T     3T   66%\\n"
     ... ).show_capacities())
     ( 8/10)  7/ 9 [ 6/10]
       6/11   0/ 8   8/ 9
      28/32   7/11   6/ 9
     """
     (min_x, min_y), (max_x, max_y) = min_and_max_tuples(self.nodes)
     width = max(len(str(node.size)) for node in self.nodes.values())
     capacity_format = f"{{: >{width}}}/{{: >{width}}}"
     return "\n".join(
         "".join(
             (
                 "({})"
                 if position == target else
                 "[{}]"
                 if position == self.position else
                 " {} "
             ).format(
                 capacity_format.format(node.used, node.size)
                 if node else
                 " "
             )
             for position, node in (
                 (position, self.nodes.get(position))
                 for position in (
                     Point2D(x, y)
                     for x in range(min_x, max_x + 1)
                 )
             )
         ).rstrip()
         for y in range(min_y, max_y + 1)
     )
Exemplo n.º 9
0
 def show(self):
     """
     >>> print(Numpad.standard_9_buttons().show())
     1 2 3
     4 5 6
     7 8 9
     >>> print(Numpad.standard_13_buttons().show())
         1
       2 3 4
     5 6 7 8 9
       A B C
         D
     """
     (min_x, min_y), (max_x, max_y) = \
         min_and_max_tuples(self.button_by_position)
     # noinspection PyArgumentList
     return "\n".join(" ".join(
         self.button_by_position.get(Point2D(x, y), " ")
         for x in range(min_x, max_x + 1)).rstrip()
                      for y in range(min_y, max_y + 1))
Exemplo n.º 10
0
 def show(self) -> str:
     """
     >>> print(Life.from_life_text(
     ...     ".#.#.#\\n"
     ...     "...##.\\n"
     ...     "#....#\\n"
     ...     "..#...\\n"
     ...     "#.#..#\\n"
     ...     "####..\\n"
     ... ).show())
     .#.#.#
     ...##.
     #....#
     ..#...
     #.#..#
     ####..
     """
     (min_x, min_y), (max_x, max_y) = min_and_max_tuples(self.grid)
     return "\n".join("".join(self.SHOW_MAP[self.grid[Point2D(x, y)]]
                              for x in range(min_x, max_x + 1))
                      for y in range(min_y, max_y + 1))
Exemplo n.º 11
0
 def show(self, solution: Optional[Iterable[Position]] = None) -> str:
     """
     >>> print(Maze(10).show())
     <BLANKLINE>
     >>> print(Maze(10).fill(product(range(10), range(7))).show())
     .#.####.##
     ..#..#...#
     #....##...
     ###.#.###.
     .##..#..#.
     ..##....#.
     #...##.###
     """
     _, (max_x, max_y) = min_and_max_tuples(self.contents, Point2D(0, 0))
     if solution:
         solution = set(solution)
     if not solution:
         solution = set()
     return "\n".join("".join(
         self.SHOW_SOLUTION if Point2D(x, y) in
         solution else self.SHOW_MAP[self.contents.get(Point2D(x, y))]
         for x in range(max_x + 1)).rstrip() for y in range(max_y + 1))
 def show(self) -> str:
     """
     >>> print(Layout.from_layout_text(
     ...     "###########\\n"
     ...     "#0.1.....2#\\n"
     ...     "#.#######.#\\n"
     ...     "#4.......3#\\n"
     ...     "###########\\n"
     ... ).show())
     ###########
     #0.1.....2#
     #.#######.#
     #4.......3#
     ###########
     """
     (min_x, min_y), (max_x, max_y) = min_and_max_tuples(self.spaces)
     reverse_locations = self.get_reverse_locations()
     return "\n".join("".join(
         str(reverse_locations[position]) if position in
         reverse_locations else "." if position in self.spaces else "#"
         for position in (Point2D(x, y)
                          for x in range(min_x - 1, max_x + 2)))
                      for y in range(min_y - 1, max_y + 2))
 def plot(
     cls,
     scanners: List["Scanner2D"],
     scanner_positions: List[Point2D],
 ) -> str:
     """
     >>> print(":", Scanner2D.plot(Scanner2DSet.from_scanners_text('''
     ...     --- scanner 0 ---
     ...     0,2
     ...     4,1
     ...     3,3
     ...
     ...     --- scanner 1 ---
     ...     -1,-1
     ...     -5,0
     ...     -2,1
     ... ''').scanners, [Point2D(0, 0), Point2D(5, 2)]))
     : ...B..
     B....S
     ....B.
     S.....
     """
     beacons = [
         beacon.offset(position)
         for scanner, position in zip(scanners, scanner_positions)
         for beacon in scanner.beacons
     ]
     (min_x, min_y), (max_x, max_y) = min_and_max_tuples(
         chain(
             scanner_positions,
             beacons,
         ))
     return "\n".join(
         "".join("S" if point in scanner_positions else "B" if point in
                 beacons else "." for x in range(min_x, max_x + 1)
                 for point in [Beacon2D(x, y)])
         for y in range(max_y, min_y - 1, -1))
Exemplo n.º 14
0
 def from_points(cls, points: Iterable[Point2D]) -> "Area":
     _min, _max = min_and_max_tuples(points)
     return cls(
         min=Point2D(_min),
         max=Point2D(_max),
     )
 def __str__(self) -> str:
     (min_x, min_y), (max_x, max_y) = min_and_max_tuples(self.risks)
     return "\n".join("".join(
         str(self.risks.get(Point2D(x, y), "."))
         for x in range(min_x, max_x + 1)) for y in range(min_y, max_y + 1))