55 lines
1.2 KiB
Python
55 lines
1.2 KiB
Python
def read_file():
|
|
with open("input.txt","r") as f:
|
|
return f.read().split('\n')
|
|
|
|
def find_row(rowpart):
|
|
row = [*range(128)] # create a list using unpack operator
|
|
for c in rowpart:
|
|
if c == 'F':
|
|
row = row[:len(row)//2] # lower half
|
|
elif c == 'B':
|
|
row = row[len(row)//2:] # upper half
|
|
else:
|
|
break
|
|
return row[0] # extract from list
|
|
|
|
def find_col(colpart):
|
|
col = [*range(8)] # create a list using unpack operator
|
|
for c in colpart:
|
|
if c == 'L':
|
|
col = col[:len(col)//2] # lower half
|
|
elif c == 'R':
|
|
col = col[len(col)//2:] # upper half
|
|
else:
|
|
break
|
|
return col[0] # extract from list
|
|
|
|
def id(seat):
|
|
rowpart = seat[:-3]
|
|
colpart = seat[7:]
|
|
row = find_row(rowpart)
|
|
col = find_col(colpart)
|
|
return 8*row + col
|
|
|
|
def part1(seats):
|
|
max_id = 0
|
|
for seat in seats:
|
|
if id(seat) > max_id:
|
|
max_id = id(seat)
|
|
return max_id
|
|
|
|
def part2(seats):
|
|
ids = sorted(map(id, seats))
|
|
prev_id = 0
|
|
for seat_id in ids:
|
|
if seat_id - prev_id == 2: # gap detected
|
|
return seat_id - 1 # so your seat is the one right before
|
|
prev_id = seat_id
|
|
|
|
def main():
|
|
seats = read_file()
|
|
print(f'{part1(seats)=}')
|
|
print(f'{part2(seats)=}')
|
|
|
|
if __name__ == "__main__":
|
|
main() |