Day 11: Seating System

Functions:

list_neighbourhood(i, j, height, width)

List all the neighbours of the given seat at position i, j.

apply(layout)

Compute a single iteration.

apply_until_stable(layout)

Run the simulation until the layout does not change anymore.

parse_layout(lines)

Parse the initial layout given as lines.

repr_layout(layout)

Represent the layout as string for debugging.

count_occupied(layout)

Count the number of occupied seats in the layout.

Classes:

Layout(table)

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 and width 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:

table

matrix of the layout

height

height of the layout

width

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

repr_layout(layout: Layout) str[source]

Represent the layout as string for debugging.

count_occupied(layout: Layout) int[source]

Count the number of occupied seats in the layout.