Day 5: Binary Boarding

Functions:

apply(first, last, directive)

Apply the directive given the range as [first, last] (inclusive).

determine_row(identifier)

Compute the row of the seat identified by the identifier.

determine_column(identifier)

Compute the column of the seat identified by the identifier.

determine_row_and_column(identifier)

Compute the (row, column) of the seat identified by the identifier.

determine_id(row, column)

Compute the identifier of the seat given its row and column.

apply(first: int, last: int, directive: str) Tuple[int, int][source]

Apply the directive given the range as [first, last] (inclusive).

Returns

new first, new last

Requires
  • re.match(r"^[FBLR]Z", directive)

  • first >= 0

  • last >= 1

  • first % 2 == 0

  • last % 2 == 1

  • first < last

  • (last - first + 1) % 2 == 0

    (Range always divisible by 2)

Ensures
  • result[0] != result[1]result[1] % 2 == 1

    (next last always not divisible by 2 unless the last step)

  • result[0] != result[1]result[0] % 2 == 0

    (next first always divisible by 2 unless the last step)

  • first <= result[0] <= result[1] <= last

  • not (directive in "BR")
    or (first < result[0] and result[1] == last)
    

    (Only first increases on B/R.)

  • not (directive in "FL")
    or (result[0] == first and result[1] < last)
    

    (Only last decreases on F/L.)

determine_row(identifier: str) int[source]

Compute the row of the seat identified by the identifier.

Requires
  • re.match(r"^[FB]{7}Z", identifier)

Ensures
  • 0 <= result <= 127

determine_column(identifier: str) int[source]

Compute the column of the seat identified by the identifier.

Requires
  • re.match(r"^[LR]{3}Z", identifier)

Ensures
  • 0 <= result <= 127

determine_row_and_column(identifier: str) Tuple[int, int][source]

Compute the (row, column) of the seat identified by the identifier.

Requires
  • re.match(r"^[FB]{7}[LR]{3}Z", identifier)

Ensures
  • 0 <= result[1] <= 8

  • 0 <= result[0] <= 127

determine_id(row: int, column: int) int[source]

Compute the identifier of the seat given its row and column.

Requires
  • 0 <= column <= 8

  • 0 <= row <= 127

Ensures
  • result <= 127 * 8 + 8

    (The highest seat ID)