I'm going to keep this one short as I've run out of ways to make this interesting. Posting this for completeness.
The challenge talks about a weird seating index scheme that uses binary partitioning. However, fancy words aside, they are literally talking about using binary.
One of the examples given was:
BFFFBBFRRR: row 70, column 7, seat ID 567
ignoring the superfluous information about rows and columns (which don't appear anywhere in the challenge), we just need to know how to convert BFFFBBFRRR into the number 567.
Basically it's binary. if you replace all the B and R with 1, and the F and L with 0, then
BFFFBBFRRR turns into
1000110111 which is 567 in binary.
We can do the converting into binary using some simple
replace() functions, and then we can turn the ascii string into binary using python's existing
int(value, 2) function. The second argument being the base system.
So, quite simply:
int(entry.replace("B", "1").replace("F", "0").replace("R", "1").replace("L", "0"), 2)
Will give you the seat number from any given string. We can quickly scan the input data using this and list comprehension:
seats = [int(entry.replace("B", "1").replace("F", "0").replace("R", "1").replace("L", "0"), 2) for entry in open("input.txt").readlines()]
The first part of the question asks for the highest seat number in the list. We simply have to do
max(seats) to find out
print("highest seat", max(seats))
The second part of the question says that there is a missing seat somewhere in the middle, but to also ignore the missing seats at either end of the range. We can use set comprehension again for this:
print("my seat", set(range(min(seats), max(seats))).difference(seats))