• Skip to main content
  • Skip to footer
  • Home
  • Become An Aficionado (Join Our Mailing List)
BMA

BeMyAficionado

Inspire Affection

Ad example

Advent Of Code 2020 – Day 5 – Binary Boarding

December 16, 2020 by varunshrivastava Leave a Comment

You’ve made it through the passport verification phase. Now, it’s time to board the plane. You board the plane but then you realize that you have dropped your boarding pass. So, how are you going to handle this situation. Well, you are a developer, you know only one thing and that is to write code. That is what you are going to do.

Somehow, you scanned all the nearby boarding passes using your mobile camera (Your Puzzle Input) ; next step is to find your seat through the process of elimination.

This airline uses binary partitioning to seat people. A seat is specified in the form FBFBBFFRLR, where F means “Front”, B means “Back”, L means “Left” and R means “Right”.

Here’s how the seating arrangements works.

Seating Arrangement of the advent of code 2020 Airplane
Seating Arrangement

Part One – Find Unique Seat Id

Always read the given problem carefully.

I don’t know if you have already seen it or not. The given seat number can be re-written in binary.

For example – this FBFBBFFRLR can be written as 0101100 and RLR can be written as 111. That’s the binary representation of rows and columns.

Now, the problem has become simple. All we have to do is to convert the binary number into their respective decimal form.

We know that the rows and columns are fixed and represented by 7 and 3 digits respectively. Therefore, we know that the maximum value of the rows can go up to about 64 and for columns it is 4. So, with this understanding, let’s write the code to identify the rows and columns.

Identify Rows

We start with 64 as the base value and with every iteration we half the value. And whenever we encounter B we add the value to the row variable.

Let me show you the code and then we will do a dry run.

   1 def find_row(seat = "FBFBBFFRLR"):
   2     a = 64
   3     row = 0
   4     for i in range(0, len(seat) - 3):
   5         if seat[i] == 'B':
   6             row += a
   7
   8         a = a//2
   9
  10     return row

Let’s create a table with all the changing variables,

irowa
0032
13216
2328
3404
4442
5441
6440
Dry run of the code to find the row

As you can see at the end we get the correct row value and that is 44.

Great!!! so we have the correct row. Now, we have to find the correct column. And for that we have to do the exact same thing but for the last 3 characters of our seat number. The code will be almost identical.

  13 def find_col(seat = "FBFBBFFRLR"):
  14     b = 4
  15     col = 0
  16     for i in range(len(seat) - 3, len(seat)):
  17         if seat[i] == 'R':
  18             col += b
  19
  20         b = b // 2
  21
  22     return col

This code will give us the column value for the last 3 characters i.e. RLR.

Now, there is a straight formula to find the seat id if we have the row and col. We will write a one liner function for that as well.

  24 def get_seat_id(row, col):
  25     return (row * 8) + col

Cool!!!

With this in place, we are good to go.

But wait, we are yet to find our place in the airplane and that is the Part two of the problem.

Part Two – Find Your Seat

Out of hundreds of Id’s you know that your seat wasn’t at the very front or back, though; the seats with IDs +1 and -1 from yours will be in your list. So, we know its somewhere in between, so if I find two ids which when subtracted from each other gives the output as 2 then that’s my seat.

Let’s quickly code the same for all the ids and find out our seat.

  31 seats = []
  32 f = open("inputs/day5.txt")
  33 for line in f:
  34     line = line.strip()
  35     seat_id = get_seat_id(find_row(line), find_col(line))
  36     seats.append(seat_id)
  37
  38 
  39
  40 my_seat = None
  41 for id in sorted(seats):
  42     if  id+1 not in seats and id + 2 in seats:
  43         my_seat = id + 1
  44
  45 print(my_seat)

The seat id that I got is 548, that is correct.

Awesome. You have found the correct seat id, perhaps you won’t disturb anyone now.

See ya next day.

Related

Filed Under: Programming Tagged With: aoc-2020, binary-boarding, day-5, problem-solving

Footer

Become an Aficionado

BeMyAficionado is all about helping you connect, grow, and make an impact—one idea at a time.

Join our mailing list for exclusive how-to guides, insider stories, and free e-books.

Get first access to new posts, tools and resources we only share with subscribers.

Join 874 other subscribers

Recent

  • Is The Cosmos a Vast Computation?
  • Building Semantic Search for E-commerce Using Product Embeddings and OpenSearch
  • Leader Election with ZooKeeper: Simplifying Distributed Systems Management
  • AWS Serverless Event Driven Data Ingestion from Multiple and Diverse Sources
  • A Step-by-Step Guide to Deploy a Static Website with CloudFront and S3 Using CDK Behind A Custom Domain

Search

Tags

Affordable Hosting algorithms amazon aoc-2020 believe in yourself best database earn money blogging education elementary sorting algorithms experience fashion finance Financial Freedom food friends goals google india indian cuisine indian education system java life life changing love make money microservices motivation oops podcast poor education system principles of microservices problem-solving programmer programming python reality seo spring success success factor technology top 5 typescript wordpress

Copyright © 2025 · Be My Aficionado · Log in

Go to mobile version