78 lines
1.7 KiB
Python
78 lines
1.7 KiB
Python
import sys
|
|
import typing
|
|
|
|
|
|
def main() -> int:
|
|
highest_seat = 0
|
|
|
|
taken_seats = set()
|
|
|
|
for line in sys.stdin:
|
|
line = line.strip()
|
|
if line == "":
|
|
continue
|
|
|
|
seat = _locate_seat(line)
|
|
|
|
if seat.number > highest_seat:
|
|
highest_seat = seat.number
|
|
|
|
taken_seats.add(seat.number)
|
|
|
|
print(f"bp={line} row={seat.row} column={seat.col} seat={seat.number}")
|
|
|
|
seat_number: typing.Optional[int] = None
|
|
n_found = 0
|
|
|
|
for candidate_seat in range(0, (127 * 8) + 1):
|
|
if candidate_seat in taken_seats:
|
|
continue
|
|
|
|
if (candidate_seat - 1) in taken_seats and (candidate_seat + 1) in taken_seats:
|
|
seat_number = candidate_seat
|
|
n_found += 1
|
|
|
|
print(f"highest_seat={highest_seat} seat_number={seat_number} n_found={n_found}")
|
|
|
|
return 0
|
|
|
|
|
|
class Seat:
|
|
row: int
|
|
column: int
|
|
|
|
def __init__(self, row: int = 0, col: int = 0):
|
|
self.row = row
|
|
self.col = col
|
|
|
|
@property
|
|
def number(self) -> int:
|
|
return (self.row * 8) + self.col
|
|
|
|
|
|
def _locate_seat(bp: str) -> Seat:
|
|
rows = list(range(0, 128))
|
|
cols = list(range(0, 8))
|
|
|
|
row_part = list(bp[:7])
|
|
col_part = list(bp[7:])
|
|
|
|
return Seat(
|
|
row=_bisect(rows, [{"F": 0, "B": 1}[s] for s in row_part]),
|
|
col=_bisect(cols, [{"L": 0, "R": 1}[s] for s in col_part]),
|
|
)
|
|
|
|
|
|
def _bisect(initial_selection: typing.List[int], bisections: typing.List[int]) -> int:
|
|
selection = initial_selection[:]
|
|
|
|
for bisection in bisections:
|
|
halfway = int(len(selection) / 2)
|
|
selection = [selection[:halfway], selection[halfway:]][bisection]
|
|
|
|
return selection[0]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|