• Skip to main content
  • Skip to primary sidebar
BMA

BeMyAficionado

Inspire Affection

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

Primary Sidebar

Subscribe to Blog via Email

Do you enjoy the content? Feel free to leave your email with me to receive new content straight to your inbox. I'm an engineer, you can trust me :)

Join 874 other subscribers

Latest Podcasts

Recent Posts

  • 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

Recent Comments

  • Varun Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Vaibhav Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Varun Shrivastava on Should Girls Wear Short Clothes?
  • D on Should Girls Wear Short Clothes?
  • disqus_X5PikVsRAg on Basic Calculator Leetcode Problem Using Object-Oriented Programming In Java

Categories

  • Blogging
  • Cooking
  • Fashion
  • Finance & Money
  • Programming
  • Reviews
  • Software Quality Assurance
  • Technology
  • Travelling
  • Tutorials
  • Web Hosting
  • Wordpress N SEO

Archives

  • November 2024
  • September 2024
  • July 2024
  • April 2024
  • February 2024
  • November 2023
  • June 2023
  • May 2023
  • April 2023
  • August 2022
  • May 2022
  • April 2022
  • February 2022
  • January 2022
  • November 2021
  • September 2021
  • August 2021
  • June 2021
  • May 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016

Tags

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

Copyright © 2025 · Be My Aficionado · WordPress · Log in

Go to mobile version