-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·61 lines (45 loc) · 1.52 KB
/
main.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
56
57
58
59
60
61
#!/usr/bin/python
import argparse
def binary_search(sequence, size, start):
bounds = (0, size - 1)
for i in sequence:
min, max = bounds
delta = (max - min + 1) / 2
if i == start:
bounds = (min, max - delta)
else:
bounds = (min + delta, max)
min, max = bounds
if min != max:
raise Exception('Could not find spot: %s / %s' % (sequence, size))
return min
class BoardingPass(object):
def __init__(self, sequence):
self.row = binary_search(sequence[:7], 128, 'F')
self.column = binary_search(sequence[7:], 8, 'L')
def __repr__(self):
return '[%s, %s]' % (self.row, self.column)
def id(self):
return self.row * 8 + self.column
def main():
parser = argparse.ArgumentParser()
parser.add_argument('file')
parser.add_argument('--next', action='store_true')
args = parser.parse_args()
with open(args.file, 'r') as input_file:
lines = [l.strip() for l in input_file.readlines()]
passes = [BoardingPass(line) for line in lines]
if args.next:
pass_ids = [p.id() for p in passes]
missing_ids = [i for i in range(0, 8 * 128) if i not in pass_ids]
for id in missing_ids:
if id - 1 >= 0 and id - 1 not in pass_ids:
continue
if id + 1 >= 0 and id + 1 not in pass_ids:
continue
print(id)
else:
max_id = max(p.id() for p in passes)
print(max_id)
if __name__ == "__main__":
main()