Day 20: Jurassic Jigsaw¶
Data:
Express the edge of a tile |
Functions:
|
Flip the side. |
|
Produce the tile transformations by rotating and flipping it. |
|
Try to assemble the remaining tiles into the image. |
|
Assemble the tiles given as ID 🠒 tile transformations into an image. |
|
Parse the |
|
Parse the input |
|
Execute the main routine. |
Classes:
|
Represent a tile of the puzzle. |
|
Represent a (partially or fully) assembled puzzle of tiles. |
|
Represent lines to conform to valid tile text. |
- VALID_SIDE_RE = re.compile('[.#]{10}')¶
Express the edge of a tile
- reverse_side(side: str) str [source]¶
Flip the side.
- Requires
VALID_SIDE_RE.fullmatch(side)
- Ensures
re.fullmatch(r"[.#]{10}", result)
- class Tile(top: str, right: str, bottom: str, left: str)[source]¶
Represent a tile of the puzzle.
Methods:
__init__
(top, right, bottom, left)Initialize with the given values.
rotate
()Copy the tile and rotate it clock-wise.
Copy the tile and flip the it along the vertical axis.
Copy the tile and flip it along the horizontal axis.
__repr__
()Represent the tile as string for easier debugging.
__eq__
(other)Compare by sides, if
other
is aTile
.Attributes:
Top side
Right side
Bottom side
Left side
- __init__(top: str, right: str, bottom: str, left: str) None [source]¶
Initialize with the given values.
- Requires
left[-1] == top[0]
bottom[-1] == left[0]
right[-1] == bottom[0]
top[-1] == right[0]
all( VALID_SIDE_RE.fullmatch(side) for side in (top, right, bottom, left) )
- top: Final[str]¶
Top side
- right: Final[str]¶
Right side
- bottom: Final[str]¶
Bottom side
- left: Final[str]¶
Left side
- transform_tile(tile: Tile) Set[Tile] [source]¶
Produce the tile transformations by rotating and flipping it.
- class Image(width: int, tiles: List[Tuple[int, Tile]])[source]¶
Represent a (partially or fully) assembled puzzle of tiles.
Attributes:
Total width of the image
Assembled tiles
Methods:
pop
()Remove the last tile from the puzzle.
attempt_add
(tile_id, tile)Try to add the tile into the image.
__eq__
(other)Return self==value.
__init__
(width, tiles)__repr__
()Return repr(self).
- width: int¶
Total width of the image
- attempt_add(tile_id: int, tile: Tile) bool [source]¶
Try to add the tile into the image.
- Returns
True if successful
- __eq__(other)¶
Return self==value.
- __repr__()¶
Return repr(self).
- place_remaining_tiles(image: Image, tiles: Dict[int, Set[Tile]]) bool [source]¶
Try to assemble the remaining tiles into the image.
- Returns
True if there are no more tiles left, or if the assembly was possible.
- place_tiles(tiles: Dict[int, Set[Tile]]) Optional[Image] [source]¶
Assemble the tiles given as ID 🠒 tile transformations into an image.
- Returns
Image, if possible; None if no puzzle could be assembled
- Requires
int(math.sqrt(len(tiles))) ** 2 == len(tiles)
(Number of tiles must be a perfect square)
- class ValidTileText(lines: Sequence[str])[source]¶
Represent lines to conform to valid tile text.
Methods:
__new__
(cls, lines)Ensure the properties on the
lines
.Get the line(s) at the given index.
__len__
()Return the number of the lines.
__iter__
()Iterate over the lines.
- static __new__(cls, lines: Sequence[str]) ValidTileText [source]¶
Ensure the properties on the
lines
.- Requires
len(lines) == 11 and re.match(r"Tile (\d+)", lines[0]) is not None and all(VALID_SIDE_RE.fullmatch(line) for line in lines[1:])
(Raise
ValueError
)
- __getitem__(index: int) str [source]¶
- __getitem__(index: slice) ValidTileText
Get the line(s) at the given index.
- parse_tile(lines: ValidTileText) Tuple[int, Tile] [source]¶
Parse the
lines
into (ID number, tile