• Skip to main content
  • Skip to primary sidebar
  • Home
  • About
  • Subscribe BMA
  • Contact Us!

Be My Aficionado

Inspire Affection

You are here: Home / Programming / Advent Of Code 2020 – Day 5 – Binary Boarding

Advent Of Code 2020 – Day 5 – Binary Boarding4 min read

December 16, 2020 by Varun Shrivastava 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.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • More
  • Click to print (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to share on Telegram (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Skype (Opens in new window)
  • Click to email this to a friend (Opens in new window)

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

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Featured Posts

Interview Attires for Men – How to dress for an Interview?

October 2, 2016 By Varun Shrivastava 2 Comments

Farewell Wardrobe

May 23, 2016 By Priyanka Yadav 1 Comment

Time – Currency of The New Rich

March 5, 2017 By Varun Shrivastava Leave a Comment

Best Exam Tips for Every Student

June 5, 2016 By Varun Shrivastava Leave a Comment

Best Javascript Framework 2017

March 2, 2017 By Varun Shrivastava 1 Comment

Latest Posts

  • Study Abroad Destinations : Research and Review
  • Advent Of Code 2020 – Day 7 – Handy Haversacks
  • Advent Of Code 2020 – Day 6 – Custom Customs
  • Advent Of Code 2020 – Day 5 – Binary Boarding
  • Advent Of Code 2020 – Day 4 – Passport Processing

Categories

  • Blogging (101)
  • Cooking (11)
  • Fashion (7)
  • Finance & Money (12)
  • Programming (50)
  • Reviews (2)
  • Technology (22)
  • Travelling (4)
  • Tutorials (12)
  • Web Hosting (8)
  • Wordpress N SEO (19)

Follow us on facebook

Follow us on facebook

Grab the Deal Now!

Hostgator Starting @$3.95/mo

DigitalOcean Free Credits

Trending

Affordable Hosting amazon aoc-2020 bad luck believe in yourself best database earn money blogging education experience fashion finance Financial Freedom food friends goals google india indian cuisine indian education system java javascript life life changing love make money microservices motivation oops poor education system principles of microservices problem-solving programmer programming reality search engines seo SSD Hosting success technology tips top 5 web web developer wordpress

Copyright © 2021 · BeMyAficionado by Varun Shrivastava · WordPress

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.