Day 5 of 2020 done in Python
This commit is contained in:
parent
5e94b4e474
commit
904241c081
2 changed files with 866 additions and 0 deletions
42
2020/Day 5/Solution.py
Normal file
42
2020/Day 5/Solution.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
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
|
Loading…
Add table
Add a link
Reference in a new issue