Advent-of-Code/2020/Day 5/Solution.py

43 lines
1.2 KiB
Python
Raw Permalink Normal View History

2020-12-17 16:57:44 +01:00
from math import floor,ceil
input_file = "input.txt"
seats = []
with open(input_file) as boardings:
line = boardings.readline()
while line and line != "\n":
row_start = 0
row_end = 127
column_start = 0
column_end = 7
for character in line:
if character == "F":
row_end = floor((row_start+row_end)/2)
elif character == "B":
row_start = ceil((row_start+row_end)/2)
elif character == "L":
column_end = floor((column_start+column_end)/2)
elif character == "R":
column_start = ceil((column_start+column_end)/2)
else: # End of line
seats.append(row_end*8+column_end)
line = boardings.readline()
print(f"Highest seat ID : {max(seats)}")
seats.sort()
possible_seats = list(range(seats[0], seats[-1]+1))
# There might be missing seats at the front or the back of the plane
# thus, if we sort and create a range between the smallest and largest ID
# and iterate over both, the number missing in the full range would be our seat.
for i in range(len(seats)):
if seats[i] != possible_seats[i]:
print(f"Your seat is {possible_seats[i]}")
break