11
votes
Day 9: Encoding Error
Today's problem description: https://adventofcode.com/2020/day/9
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 got a little bit weird. I've been writing up tips and thoughts on previous posts. I hope y'all are enjoying them. I also decided to stop calling them 'Tips for Beginners' since that feels a bit patronizing.
Day 9 Part A – Python
I built a buffer that I just loaded my preamble into. As I moved along, I just pushed the old values off the front and added the new ones on to the back. Then I built a check_sums method that searched the buffer for a sum that worked. Assuming there would never be a duplicate in my buffer, which ended up being correct. But I was a bit worried if that wasn't as safe assumptionDay 9 Part B – Python
I stepped through the entire data set. For each row, I moved onward from it adding numbers and checking if it hits the target value. I don't like how it keeps checking even after I've found or exceeded the target, but for a dataset of this size it does the job. I might go back and improve this oneTips and Commentary
For part A you'll probably want a buffer, which stores a bunch of data for you. An easy way to manipulate your buffer is through push and pop which allow you to manipulate specific locations within the buffer. They might go by different names, like in Python I used a combination of pop() and append()
My part B solution is hellishly inefficient, I might want to redo it to make it terminate early upon completion or determination that a given chain is no longer viable. In mine I loop through a bunch of data looking for valid combinations. But I keep running even after I've found my answer. It spends for more time and energy than is necessary. which is why I say it's inefficient. It should stop searching once it finds a satisfactory result
You can actually quantify efficiency in computer science. The term is Big O notation. For example, my search could be represented as O(n^2) which means that the computational power used scales exponentially with the data set. Which is not good.
Python
Repo Link
Part 1
Basically, I just checked if the current number was in one of the combinations of the current window of numbers. I used Python's itertools.combinations to generate all the combinations of the current window (maintained with a fixed-sized collections.deque)
Part 2
I'm pretty sure there is a more efficient way to find the contiguous array, but I was lazy and just brute-forced it.
Your solution for task one is so elegant.
Just goes to show that I don't know enough about the tools available in python to make my life easier. xD
Python
The problem was a little boring in my opinion today... but backstory made it interesting nonetheless!
Repo Link
Part 2
Awesome linear time solution for
find_ctg
. I've updated my solution for Part B to use a variation of your approach.This was fun, kind of like a sequel to day 1's problem; my part 1 solution is almost exactly the same.
Part 1
Part 2
Sheets!
Both of these formulas were drag-fills, which I am not happy with -- but the fact that I can still do anything in a spreadsheet is great.
Below are the formulas for the answer rows. I ditched the others since they're pointless to run.
I really didn't think I'd be able to do anything after yesterday's puzzle. So far as I can see, Sheets can't do that sort of stuff. I am hopeful that every odd day from here on out (except for the 25th) won't require that intputer business. We'll see how it goes.
This method is pretty hacky.
Part 1
Part 2
and then in a second column I used
This generates ranges with an offset. Not the prettiest thing, but it does work.
In L2 I had the offset. I had to hunt a bit, but I lucked out by starting at 15 and struck gold at 16. At the top of the sheet I ran a filter to get the answer.
Witness the magic
Todays problem seems suited for python and numpy to make a triumphant return :)
Managed to get part two down to just a few lines which was nice!
Part 1
Part 2
Chicken Scheme
Only interesting thing here is using a generator to lazily calculate the sums of pairs of the preamble.
Posting mine again for today. I find it a nice distraction but can't be bothered with cleanup. I haven't checked but if past days are any indication, someone in here will have solved this in two lines of creative map() uses when it took me over 40 lines of oldschool for loops with indexes. Overall this wasn't so hard, though.
Part 1+2
This one was not too bad, and I liked revisiting day 1 with a twist.
Part 1
Part 2 diff
PC vs Pi Performance
This was an interesting one, although I agree with @nothis in saying that I definitely did this one the "oldschool" way... I found that it helped keep my mind straight as far as the iterators, though, and being able to quickly subset the array that way, instead of trying to use javascript's .filter() or .splice() :)
Part 1
Part 2
This one was only difficult because I tried to do a bunch of things to avoid unnecessary computation. Elixir's
Enum
functions are generally better suited to working through the whole enumerable—in cases where you need to both maintain some kind of state while iterating through the enumerable and stop short once you've found what you're looking for, it can get a little messy.I think there might be a cleaner way to accomplish what happens in the
Enum.reduce_while
calls usingStream
functions; might revisit this later.Edit: There was indeed a cleaner (or at least shorter) way using
Stream
functions. Added the new definitions in the second expander.Parts 1 and 2, Elixir
Cleaner/shorter solution using Stream functions
Ruby
I suspect there's some kind of sliding-window iterator available somewhere in ruby that would make the first part cleaner, but at least I have
combination
to make searching the "preamble" for valid numbers easier. Ruby is a bit like Python in that it's very "batteries included" and saves a lot of time on puzzles like this.Part 1
Part 2
I had a lot of trouble figuring out how I could take an iterator in Rust and split it across two loops, since most of the methods on iterators take ownership of the iterator. Thankfully, I stumbled upon
by_ref
!Also, I know that I'm parsing the
lines
tonums
twice, for some reasontake
ing after collecting and recreating the iterator wasn't working, and I'm too sleepy to figure out why.lib.rs
main.rs
MIT Scheme. Could have made part one a lot more efficient but decided to just use my two sum function.
Part 1 & 2
Part 1 alt
Re-implemented part one to be 3 times faster and 9 times uglier.Rust
I like problems like this where the resulting code can be (relatively) nice and clean.
Rust