-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5-2.py
70 lines (61 loc) · 1.82 KB
/
day5-2.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
65
66
67
68
69
70
import math
from operator import itemgetter
records = []
def readInput(file):
codes = []
file = open(file, "r")
for line in file.readlines():
codes.append(line.rstrip())
print('%d lines imported' % len(codes))
file.close()
return codes
for code in readInput('day5-input.txt'):
valid = True
rowMin = 0
rowMax = 127
colMin = 0
colMax = 7
seatID = None
for index in range(len(code)):
# determine the row
if index >= 0 and index <= 6:
if code[index] == 'F':
rowMax = math.floor((rowMax - rowMin) / 2) + rowMin
elif code[index] == 'B':
rowMin = math.ceil((rowMax - rowMin) / 2) + rowMin
else:
valid = False
# determine the column
elif index >= 7 and index <= 9:
if code[index] == 'L':
colMax = math.floor((colMax - colMin) / 2) + colMin
elif code[index] == 'R':
colMin = math.ceil((colMax - colMin) / 2) + colMin
else:
valid = False
else:
valid = False
if valid == False:
print('Record is invalid')
# determine the seat ID
seatID = (rowMin * 8) + colMin
record = [code, rowMin, colMin, seatID]
if valid == True:
records.append(record)
print(f'{record} added')
else:
print('Code was invalid.')
sortedRecords = sorted(records, key=itemgetter(3))
maxSeatID = sortedRecords[-1][3]
print(f'\nThe highest seat ID is {maxSeatID}.\n')
prevSeatID = None
mySeatID = None
for record in sortedRecords:
if record == sortedRecords[0]:
prevSeatID = record[3]
else:
seatID = record[3]
if seatID != prevSeatID + 1:
mySeatID = seatID - 1
prevSeatID = seatID
print(f'My seat ID is {mySeatID}.')