Day 11: Seating System¶
Functions:
|
List all the neighbours of the given seat at position |
|
Compute a single iteration. |
|
Run the simulation until the layout does not change anymore. |
|
Parse the initial layout given as |
|
Represent the layout as string for debugging. |
|
Count the number of occupied seats in the |
Classes:
|
Represent a seat layout. |
- list_neighbourhood(i: int, j: int, height: int, width: int) List[Tuple[int, int]] [source]¶
List all the neighbours of the given seat at position
i, j
.The
height
andwidth
define the limits of the layout.- Requires
0 <= j <= width
0 <= i <= height
- Ensures
(i, j) not in result
len(result) <= 8
all( 0 <= i <= height and 0 <= j <= width for i, j in result )
- class Layout(table: List[List[str]])[source]¶
Represent a seat layout.
Methods:
__init__
(table)Initialize with the given values.
Attributes:
matrix of the layout
height of the layout
width of the layout
- __init__(table: List[List[str]]) None [source]¶
Initialize with the given values.
- Requires
all( re.fullmatch(r"[L#.]", cell) for row in table for cell in row )
len(table) > 0 and len(table[0]) > 0 and all( len(row) == len(table[0]) for row in table )
- Ensures
len(self.table) > 0 and self.width == len(self.table[0])
self.height == len(self.table)
- table: Final[List[List[str]]]¶
matrix of the layout
- height: Final[int]¶
height of the layout
- width: Final[int]¶
width of the layout
- apply(layout: Layout) Tuple[Layout, int] [source]¶
Compute a single iteration.
- Returns
(new layout, number of changes)
- Ensures
all( (cell == "." and result_cell == ".") or (cell != "." and result_cell in ["L", "#"]) for row, result_row in zip(layout.table, result[0].table) for cell, result_cell in zip(row, result_row) )
(Valid change)
layout.width == result[0].width
layout.height == result[0].height
- apply_until_stable(layout: Layout) Layout [source]¶
Run the simulation until the layout does not change anymore.
- parse_layout(lines: List[str]) Layout [source]¶
Parse the initial layout given as
lines
.- Requires
not (len(lines) > 0) or all(len(line) == len(lines[0]) for line in lines)
(Lines are a table)
all(re.match(r"^[.L#]+Z", line) for line in lines)
- Ensures
not len(lines) == 0 or all(len(line) == len(row) for line, row in zip(lines, result))
len(lines) == result.height