-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.py
64 lines (48 loc) · 1.29 KB
/
day5.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
62
63
64
#!/usr/bin/env python3
import math
filename = 'inputs/input5.txt'
def init_seat_map():
seat_map = dict()
i = 0
j = 0
while i < 128:
while j < 8:
seat_map[i * 8 + j] = 1
j += 1
j = 0
i += 1
return seat_map
def get_seat_id_fast(code):
row_code = code[:7]
col_code = code[7:]
row_bin = ''.join(['0' if i == 'F' else '1' for i in row_code])
row = int(row_bin, 2)
col_bin = ''.join(['0' if i == 'L' else '1' for i in col_code])
col = int(col_bin, 2)
sid = row * 8 + col
return sid
def get_largest_sid(codes):
largest_sid = 0
seat_map = init_seat_map()
for code in codes:
seat = get_seat_id_fast(code)
del seat_map[seat]
largest_sid = max(largest_sid, seat)
return largest_sid, seat_map
def get_my_seat_id(seat_map):
my_sid = -1
for sid in seat_map.keys():
if sid != my_sid + 1:
my_sid = sid
break
my_sid = sid
return my_sid
if __name__ == '__main__':
with open(filename, 'r') as fin:
lines = fin.read().split('\n')
# part 1
largest_sid, seat_map = get_largest_sid(lines)
print(f'Largest seat ID: {largest_sid}')
# part 2
my_sid = get_my_seat_id(seat_map)
print(f'My seat ID: {my_sid}')