13
votes
Day 5: Binary Boarding
Today's problem description: https://adventofcode.com/2020/day/5
Join the Tildes private leaderboard! You can do that on this page, by entering join code 730956-de85ce0c
.
Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python
with any of the "short names" listed in this page of supported languages):
<details>
<summary>Part 1</summary>
```python
Your code here.
```
</details>
This one was fun! There are some neat tricks you can do to dramatically reduce the complexity of this one. I could probably cut my code in half pretty easily if I took advantage of math.
Day 5 Part A - Python
I recognized this was just fancy binary, so I split it into sections and then did a find and replace to turn them from fancy binary into regular binary. From there I just did a base-2 int cast to get the numbers I needed.
Day 5 Part B - Python
I thought about a couple of different ways I could do this, but I ended up just making a quick list of all the seats, finding the min and max, and then iterating between them until I found which seat wasn't there.Tips For Beginners
There's a hint in the title: You can use binary for this one to make the math much less of a pain
If you're feeling tricky, you don't even need to separate out rows and columns. You can just directly compute the seat id (But it's good to walk through the process if you're having trouble figuring it out)
I really took advantage of the built in functions in Python. Find and replace and casting from a base 2 string to a base 10 int saved me a lot of headaches. I didn't know you could cast between bases like I used for this, but some quick googling taught me about it
Having a good grounding in math will really help you on this one. Try to remember how base 2 works, if you learned that in school. If you never covered it or don't remember, it might be a good topic to read up a bit on
Damn, I was super happy with how clever my solution was but I'm a little upset I didn't think of your way! Brilliant!
Thanks! If you want to get really fancy:
Spoilers
You don't even have to split out the rows and columns, you can just take the whole thing straight to binary and directly calculate the seat_idSpoilers
Since you know it's not at the beginning or the end and you know the seats IDs are contiguous, you don't even need to find the min and the max, just one side and go up or down until you find the first one that's not there!Oh damn, good point! My first idea was way too complicated. I almost
Spoilers
Sorted the whole list, checked the 'slope' at each point numerically by subtracting it from the next value, and looked for the peak in that janky numerical first derivativeThat would have been WAY more work than was necessary
I did something of the sort.
Spoiler
I just stopped as soon as I found the missing seat-id, I didn't go through it all:Sort the list, find the spot where the difference between seat-ids is bigger than one.
My seat-id is the missing one.
Started with haskell this time because it seemed like a good tool for the job; it was, but the whole parser business made it a bit more complicated than I would've liked.
day5.hs
I'll also join the chorus in saying that I thought this one would be harder. Specifically, I was expecting part 2 to change the whole parameters, like applying the same evaluation to a differently-sized airplane. So, I kind of over-engineered part 1 in anticipation of that. In any case, I didn't do the binary-cast-trick like some others here, which is super clever! I did mine the silly, slow way. But I was really happy that I was able to successfully use Array.map and Array.reduce to avoid a few loops in the final solutions for both parts!
Part 1
Part 2
Ruby
Part 1
Part 2
Edit
Hah, I didn't pick up on the binary trick, I tend to think too literally when reading the specs. This serves me well in some puzzles, and not so much in others :P. It's simple enough that I went back and did it that way too:
Alternate
MIT scheme solution.
Part 1 & 2
Pretty simple if you can find the trick. I wonder if scheme has a better way to do part b.Sheets!
This one was fairly straightforward for a spreadsheet, actually. Part 2 was longer than it needs to be, simply because I'm trying to do each part with a single formula.
I really liked that the seating accounted for double-rows for safety exits. A nice addition since the average passenger is over 7' tall.
Part 1
Part 2
My sheet
I hadn't even noticed! Points to you for attention to detail :)
Day 5! This one was fun, although I had to figure out how to slide an array in Elixir.
Elixir
I'm sure there's a better way to do this, but I started it late and wasted some time figuring out incompatibilities with BWK AWK.
Part 1
Part 2
I was expecting something much harder since tomorrow is a weekend!
Also, I have joined the private leaderboard as (anonymous user #573549). :)
Rust
main.rs
My solution in rust:
Was fun to do, was expecting something much harder given that it is Saturday.
Link to the repository, including test runner
main.rs
This time the solution was very straight forward so didn't do anything fancy, just pure python.
(edit: realised I could use
translate
in one go instead of usingreplace
multiple times)Part 1 in Python
Part 2 in Python
Rust
Not particularly proud of this one. I didn't pick up on the binary implementation at all. When I was doing this last night I couldn't figure out how to sort my vector of seatIDs, so I ended up printing out all seatIDs, pasting into Sheets, sorting, and then finding the part2 answer that way.
I've since figured out how to sort.
Rust
Like many others, I didn't pick up on the binary hint in the title. In fact, I wrongly assumed it referred to a binary search, which the process they outlined for determining the numbers seemed to be. Oh well, it still wasn't bad, and Julia has some fun tools that made part 2 easy even though I didn't have some elegant way of doing it.
Part 1
Part 2 diff
I may try to do some of the future challenges running on a Raspberry Pi, since I am interested to see how it performs with Julia, not sure if that interests anyone here to see.
My simple solution in Python
Part 1
Part 2 addition
Today was pleasant. Took me about 20 mins, which seems appropriate. I just checked the global leaderboards and someone did it in 00:02:55, lol. It reminds me of how hard these can get towards the end and this year I'm not sure I'll do the later ones that take a normal person like... hours.
Part 1+2
JavaScript
My initial implementation was a typical binary search and then straightforward id calculation. After reading some of the answers here and realizing it's just a binary number, here's a more interesting solution:
Parts 1 + 2
oops I dropped my boarding pass, lemme just scan 850 other boarding passes to avoid bending my knees before sitting for hours
Parts 1&2
Like a lot of other people, I figured out that
partial solution spoiler
the characters in the partitions could just be mapped to 0's and 1's, and then parsed as binary numbers.I like @markh's part 2 solution using
Enum.chunk_every/3
, but my uglier solution usingEnum.reduce_while/3
might be slightly faster ;)Part 1 and 2
Didn't do any over the weekend, so I was playing catch up today, and beating my head against the brick wall that was part B for day 4. I'm pleased to say that I nailed both parts for day 5 quite quickly though!
Parts A and B in python