-
Notifications
You must be signed in to change notification settings - Fork 0
/
boarding.py
55 lines (43 loc) · 990 Bytes
/
boarding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import click
import io
from typing import Tuple
from itertools import product
# Binary space partitioning means
# we can just convert the code into
# binary numbers.
F = 0
B = 1
L = 0
R = 1
Seat = Tuple[int, int] # row, col
ROW = 0
COL = 1
def parse(code: str) -> Seat:
row = int(''.join([
'0' if c == 'F' else '1'
for c
in code[:7]
]), 2)
col = int(''.join([
'0' if c == 'L' else '1'
for c
in code[7:]
]), 2)
return row, col
@click.command()
@click.argument('codes', type=click.File('r'))
def main(codes: io.TextIOBase):
ids_ = set()
for code in codes:
code: str = code.strip()
seat = parse(code)
id_ = (seat[ROW] * 8) + seat[COL]
ids_.add(id_)
ordered = sorted(ids_)
for i in range(0, len(ordered)):
cur, next_ = ordered[i], ordered[i+1]
if next_ != cur + 1:
print(cur + 1)
return
if __name__ == '__main__':
main()