-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommon.py
More file actions
47 lines (30 loc) · 975 Bytes
/
common.py
File metadata and controls
47 lines (30 loc) · 975 Bytes
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
from typing import List, Tuple
front = "F"
back = "B"
left = "L"
right = "R"
num_rows = 128
num_cols = 8
def parse(instr: str) -> List:
return instr.strip().split("\n")
def decode_position(row_string: str, dec_char: str, inc_char: str, max_val: int) -> int:
min_val = 0
max_val -= 1
current_range = (max_val + 1) - min_val
for char in row_string.upper():
range_modifier = current_range / 2
if char == dec_char:
max_val -= range_modifier
elif char == inc_char:
min_val += range_modifier
current_range /= 2
if row_string[-1] == dec_char:
return min_val
else:
return max_val
def parse_seat(seat_string: str) -> Tuple[int, int]:
row = decode_position(seat_string[:7], front, back, num_rows)
col = decode_position(seat_string[7:], left, right, num_cols)
return int(row), int(col)
def get_seat_id(row: int, col: int) -> int:
return (row * 8) + col