DEV Community

Cover image for Advent of Code 2020: Day 05 with Python
Yuan Gao
Yuan Gao

Posted on • Updated on

Advent of Code 2020: Day 05 with Python

I'm going to keep this one short as I've run out of ways to make this interesting. Posting this for completeness.

The Challenge Part 1

Link to challenge on Advent of Code 2020 website

The challenge talks about a weird seating index scheme that uses binary partitioning. However, fancy words aside, they are literally talking about using binary.

One of the examples given was:

BFFFBBFRRR: row 70, column 7, seat ID 567
Enter fullscreen mode Exit fullscreen mode

ignoring the superfluous information about rows and columns (which don't appear anywhere in the challenge), we just need to know how to convert BFFFBBFRRR into the number 567.

The only solution, really.

Basically it's binary. if you replace all the B and R with 1, and the F and L with 0, then BFFFBBFRRR turns into 1000110111 which is 567 in binary.

We can do the converting into binary using some simple replace() functions, and then we can turn the ascii string into binary using python's existing int(value, 2) function. The second argument being the base system.

So, quite simply:

int(entry.replace("B", "1").replace("F", "0").replace("R", "1").replace("L", "0"), 2)
Enter fullscreen mode Exit fullscreen mode

Will give you the seat number from any given string. We can quickly scan the input data using this and list comprehension:

seats = [int(entry.replace("B", "1").replace("F", "0").replace("R", "1").replace("L", "0"), 2) for entry in open("input.txt").readlines()]
Enter fullscreen mode Exit fullscreen mode

The first part of the question asks for the highest seat number in the list. We simply have to do max(seats) to find out

print("highest seat", max(seats))
Enter fullscreen mode Exit fullscreen mode

The Challenge Part 2

The second part of the question says that there is a missing seat somewhere in the middle, but to also ignore the missing seats at either end of the range. We can use set comprehension again for this:

print("my seat", set(range(min(seats), max(seats))).difference(seats))
Enter fullscreen mode Exit fullscreen mode

Tadaa!
Onwards!

Top comments (3)

Collapse
 
ljcdev profile image
ljc-dev

Lol as expected that wasn't much of a challenge to u 😁. I had such a hard time 😭.
Part 1 Got it using reduce, switch, math.round etc. Then Simon told me to use binary.
For part 2 I used a convoluted divide and conquer logic 😂. Then Simon again suggested a simpler way with Set and find.
Btw, python set difference is such an awesome feature 😍.

Collapse
 
meseta profile image
Yuan Gao

You don't have to do everything Simon says you should...oh wait, maybe you do have to do everything Simon says...

Yep, if you don't use binary, you'd have to do a lot more parsing, but it's a valid solution! Don't worry if you're finding it challenging, hopefully as you figure things out, even if it takes a while, you're learning, and also having a look at other solutions once you've done yours should help to give you more ideas to use in the future if you encounter similar issues. Good luck with the other challenges!

Collapse
 
syentix profile image
syentix

Hate to say it, but I actually did have a different solution and I totally didn't think about binary :o