13
votes
Day 3: Binary Diagnostic
Today's problem description: https://adventofcode.com/2021/day/3
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>
Python golf, day 3. I'm quite proud of part 1 especially. Again tips and improvements are appreciated.
Part 1
Part 2
Wow, yeah, the first part is really neat!
Part B felt like a pretty substantial jump in difficulty after the last 2 days that felt more like warms ups. But also it was a much more interesting challenge
Day 3 Part A – Python
Just counting up the ones in each column and then doing some binary math to get the different values. Not too fancy, but it works.Day 3 Part B – Python
This took a second to wrap my head around what it was even asking. Recursion seemed like the best bet since the list has to narrow every time. Keeping track of what type of object I was working with too a bit of tracking. And my 2 methods are basically the same, except "0" and "1" are flipped. I'm sure they could be streamlined into one method with some workTips
This one you really need to read the problem description to figure out what it's asking
The sample data is only 5 characters long, but your real data will be longer, so make sure to make you code so that it can handle longer inputs
Binary math is useful in this one, so it's helpful to refresh yourself on it
I found recursion a useful way to handle part B. If you're not familiar, recursion is a tool where a method is able to call itself. It's useful if you need to do something like look and increasingly smaller subsets of a data set
Oh yeah! I always forget collections exists. Usually when I need a bigger or more complex data structure I break out pandas, but default dicts can be extremely useful. I didn't know you could nest them like that though, that's really neat!
Gross, this is what I get for trying to go fast. Not very satisfied with either part, and I'm not really comfortable enough with Ruby's bit-bashing features to do it the way I think I'd prefer (at least not off the cuff, I'll probably come back tomorrow and revisit it). Part 1 was more or less okay, 2 was way more repetitive than I'm happy with.
But hey, I'm getting faster.
Part 1 Ruby
Part 2 Ruby
There's gotta be a better way to do this.
Part 1
This is slow, but covers part 1.
Since Sheets can only process binary up to ten digits, I had to use this old chestnut to get around it
I came in at 22:10, which ranked me at 7800 -- which is good for me. :)
Part 2
ok, I tweaked this. The first formula is
from there I pop this in the next cell and drag it across.
Not pretty.
I haven't seen what anybody else did, so I'll most likely refine it into a oner like the others.I still haven't looked, but I did do it over so its a little leaner.In at 1:03:27 and ranked 6986th -- also pretty great for me. Sheets really isn't great for some of these things.
Here's my sheet so far
[...]
I think the answer is obvious but I love that your abusing spreadsheets to do it and the cover sheet is lovely.
ha yeah. I haven't seen any of the other spreadsheet folks crank out a better method. There really must be one.
What's the obvious answer I'm missing, though? :)
I was just joking - python, ruby, rust, go, kotlin, swift ...
I've tried using spreadhseets for mildly complex calculations and it's so painful that I'm in awe that it's even possible.
doing a sexy solution for part two is killing me! I'm so close, but not quite there... and it's driving me nuts.
I believe they're suggesting you should be using MS Excel instead. ;)/s
ha. maybe. Sheets can do pretty much everything Excel can these days.
I've yet to see a better method among the spreadsheet folks. I've sorted out some better methods for the first part (below), but nothing for the second
Alternate Part 1 Method
Love the sheet front page, it's FAB-U-LOUS!
p.s. Death to Smoochy is seriously underrated. RIP RW
ha. I think that's one of the few remaining easter eggs in Sheets. If you put 'pride' across the first five columns, it'll do that. I think its super classy :)
As of last night that is the best transparent PNG of Rainbow Randolph, too. haha
When it comes to Advent of Code, copying an entire block and changing one line >>> implementing a reusable function. :-)
Part 1
Part 2
Racket
Notes
I like small, easily tested procedures and data that is easily consumed/managed. I converted each line of input into list of characters. I considered vectors but the lines were short enough it didn't seem worth it. I'll build up a list of characters for gamma and epsilon that will need converting to a number. Racket's built-in
string->number
accepts a radix so it's trivial to convert a string of bits.I'll need to count binary digits.
Originally I transposed the input with the mind-bending
(apply map list report)
- drop me a note if you want an explanation - but refactored to a positional count instead when I got to Part 2.Part 1
The only strange thing is
gamma
andepsilon
are built by pre-pending a 0 or 1 after counting 1s and 0s from the first position to the last, then reversing the results. It's a very common Lisp/Scheme/Racket idiom since lists are basically similar to linked lists, so pre-pending is constant time and appending isO(n)
. The same is true appending to strings, it's generally not worth it.Part 2
Bit rating criteria returns the bit value to check at each position (as a character)
Pass the specific criteria to the rating search which is a simple recursive search that filters out invalid ratings until only one is left. As usual, no error handling or validation, since Eric is kind enough to give us input that gives guaranteed results.
Then Part 2 is a trivial multiplication
If anyone is actually reading, let me know if I should drop the explanations and just show code since I know I'm overly verbose.
No, keep the explanations if you have the time. I don't know Racket myself, but it's fascinating to see how terse the solutions are, and the explanation definitively helps.
Python
My journey to learn Rust continues and today it sort of got the better of me. I got turned around in what I was trying to do and learn, and ended up pulling some bits from the solution @wycy. His (or Her's, or Their!) coding style is similar to mine so looking through it I was able to understand what they were doing, instead of some of the voodoo magic stuff other people are doing.
On the plus side, my understanding of Rust increased. Plus, I swear all the documentation I find when I search how to do things, makes it needlessly complicated.
Solutions in Go
Part 1
Part 2
JavaScript
Repo Link
Python is my primary language, but in order to get some practice, I've also been trying out some JavaScript.
Part 1
Part 2
Below, I'm only including the parts that are different from Part 1.
Yeah, this one seemed quite a bit harder than yesterday's.
My code could probably be more efficient and nicer, but I've spent enough time on it for today...
Part 1 — Rust
Part 2 — Rust
I think I'm finally starting to understand Rust lifetimes! I wanted to pass around a bunch of references and tried my best to avoid reallocating or moving Strings, and this is what I came up with!
Sourcehut Link
I'm still not super adept at manipulating types of references, so if anyone has any suggestions for how I could clean those up (Is there something better than
&[&String]
?) I'd love to hear feedback.Part 2 was a big oof for me. First I misunderstood it, and then I spent a lot of time wondering if I understood integer division and the answer is still only maybe. Also, the function that I reuse in part 2... didn't appear until I read part 2 lol. I also tried to be fancy using the Nim
include prelude
thing to have it import a lot of commonly used standard library stuff but it only worked for some of the imports it claims to do? Oh and I keep feeling like I am reading in the files in the least idiomatic way possible, but they changed thereadLines
function to require you to specify the number of lines, which is not too helpful.Part 1
Part 2
Haha, this one absolutely killed me. While I defeated my own purpose here in my haste to get caught up on the problems I was behind on, I did manage to get a functional solution regardless of its readability. I've already promised myself no more of this sloppy type of implementation, since my goal here is to get familiar with the more convenient parts of Node/JS and not just implement everything with indices and for loops. :)
Part 1
Part 2
Rust
Kind of gross, but it works.
Rust
Yeah, I'm not going to lie, part 2 today threw me for a little loop. The other exercises up until that point were pretty breezy, but I had to spend a good bit of time reading the requirements for this one just to figure out what I needed to do.
Common code - Javascript
Part 1 - Javascript
Part 2 - Javascript
I thought about refactoring this to a more general solution, but decided not to.