day 5
aoc 2020 » Devlog
5a.py goes through the list of seats, converts them to binary strings (from the description, F/L is a 0 and B/R is a 1), converts these to an int, and then saves this ID if it is larger than the largest seat ID found so far
# this airline uses binary space partitioning to seat people.
# A seat might be specified like FBFBBFFRLR, where
# F means "front", B means "back", L means "left", and R means "right"
# F/L lower half, B/R upper half
# What is the highest seat ID on a boarding pass?
# input
with open('5.txt', 'r') as file:
input = file.read()
# turn the input into a list; each row is a string FBFBBFFRLR
input_list = list(input.split('\n'))
# find largest
big_num = 0 # highest seat ID so far
for seat in input_list:
# convert to a binary string
seat_bin = ''
for i in range(0,len(seat)):
if seat[i] in ('F', 'L'):
seat_bin = seat_bin + '0'
elif seat[i] in ('B', 'R'):
seat_bin = seat_bin + '1'
# convert this to an int
num = int(seat_bin, 2)
# keep iff it is larger than all ones so far
if num > big_num:
big_num = num
print(big_num)5b.py goes through the list of seats, converts them to binary strings (from the description, F/L is a 0 and B/R is a 1), converts these to an int, and then adds this ID to a list of seat IDs. it then sorts this list (the code ran quickly enough that i figured just using the inbuilt .sort() function is fine), checks for consecutive pairs of seats with a difference of 2, and then returns the seat number that would be between these two.
# input
with open('5.txt', 'r') as file: input = file.read()
# turn the input into a list; each row is a string FBFBBFFRLR
input_list = list(input.split('\n'))
# get a list of all seat IDs
seats = []
for seat_str in input_list:
# convert to a binary string
seat_bin = ''
for i in range(0,len(seat_str)):
if seat_str[i] in ('F', 'L'):
seat_bin = seat_bin + '0'
elif seat_str[i] in ('B', 'R'):
seat_bin = seat_bin + '1'
# convert this to an int
seat = int(seat_bin, 2)
# add to list of seats
seats.append(seat)
# sort list
seats.sort()
# find two consecutive seats with a difference of 2
for i in range(0, len(seats)):
if seats[i+1] - seats[i] == 2:
print(seats[i] + 1)
break # found the answerFiles
5.txt 9.9 kB
Dec 05, 2020
5a.py 933 bytes
Dec 05, 2020
5b.py 1 kB
Dec 05, 2020
Get aoc 2020
aoc 2020
not a mod, just some code
| Status | Released |
| Category | Other |
| Author | riv |
| Tags | advent-of-code, advent-of-code-2020, python |
Leave a comment
Log in with itch.io to leave a comment.