Find the seat number mkay

This commit is contained in:
2021-01-10 20:41:06 -05:00
parent db6d5ad880
commit 9b90743cc1
6 changed files with 172 additions and 1 deletions

77
aoc2020/day05/solution.py Normal file
View File

@@ -0,0 +1,77 @@
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())

3
aoc2020/day05/test-input Normal file
View File

@@ -0,0 +1,3 @@
BFFFBBFRRR
FFFBBBFRRR
BBFFBBFRLL

View File

@@ -0,0 +1,3 @@
bp=BFFFBBFRRR row=70 column=7 seat=567
bp=FFFBBBFRRR row=14 column=7 seat=119
bp=BBFFBBFRLL row=102 column=4 seat=820

View File

@@ -0,0 +1,23 @@
import sys
from pathlib import Path
import pytest
from solution import main
HERE = Path(__file__).absolute().parent
def test_solution(capsys):
with (HERE / "test-input").open() as infile:
sys.stdin = infile
main()
expected_output = (HERE / "test-output").read_text().splitlines()
assert expected_output == [
l
for l in capsys.readouterr().out.splitlines()
if not l.startswith("highest_seat")
]