15
votes
Day 1: Sonar Sweep
Today's problem description: https://adventofcode.com/2021/day/1
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
Repo Link
Part 1
List comprehensions, yay!
Part 2
This took me longer than it should have, but once I realized that if I could just form the windows separately, then I could re-use the method for counting the increases from Part A.
Oh shit, I completely forgot about this! I love Advent of Code. Last year I worked through them and did a write up for as long as I was able to stick with it. And I think folks responded pretty positively to that, so I'm going to try to do it again
Day 1 Part A – Python
Just a text parser and a loop. Nothing particularly fancy
Day 1 Part B – Python
This one is slightly more tricky in that you need to combine the elements into a sliding window. This could definitely be done in place as you move through the list by keeping a running sum, but I just made a new list by adding every element to itself and the next 2 spots, then shortening the list back down to cut off the extra end pieces
Tips
The biggest challenge here is going to be getting your text parser up and running and converting your input into a form you can work with.
Part 2 builds on part 1. See how you can reuse what you've already figured out. This is going to be a recurring theme for Advent of Code. Many problems build upon other problems
There are multiple ways to do it, if you get stuck on one particular method, try something else
With early Advent of Code problems you usually don't need to worry about making your code efficient. It's still good to think about because later problems will require you to use efficient methods or your code will never finish on time.
Even if you're able to solve a problem right away, it's still good to think about other ways to approach it. They may help you out later
This is my first year participating. My goal is to improve my working Rust knowledge as well as experiment with software quality tools. To that end, I'm learning to use or use better:
Day 01 — Part A (Rust)
Day 01 — Part B (Rust)
I'm back with Sheets... seeing as I haven't learned a single thing over the past year.
Part 1
Part 2
Part 1 in Racket
I would normally use
(rest input)
to skip the first element of a list but I'm usingdrop
instead to highlight the similarity to part 2.Part 2 in Racket
It's identical but instead drop the first 3 elementsExplanation
For part 2, there's no need to sum or maintain any sliding windows since for any 4 consecutive values
[a b c d]
you have two windows with a common pair of values[a B C]
vs[B C d]
so the window sum increases whend>a
.It turns out both parts can be solved by simply comparing the list of values that lag by 1 or 3 places.
Racket's
for
can iterate through lists either nested or in parallel so you can compare consecutive values (that lag by 1 place) by iterating through the same list and dropping the first element. You can compare values that lag by 3 places by iterating through the same list but dropping the first 3 values.It works because the iteration stops when any list is empty.
I use this technique to compare consecutive items of a list regularly.
I'm going to use Javascript this year.
Part 1
Part 2
Not totally satisfied with this. I got the correct answers, but I realize I'm ignoring some efficiencies.
I'm sort of trying to re-learn Rust because I like it, so my code is pretty terrible, but here's my code for both parts (in one).
rust
I will post my solutions in Go, that means that I don't have to worry about writing anything fancy because the inability of writing fancy stuff is a Go feature, right?
Part 1
Part 2
Going with Ruby again this year. Might try to brush up a bit on my Rust as we get going, but for now I'll stick with what I know I can do fast.
Part 1
Ruby, not unlike Python, is great for this sort of thing because it's so "batteries included"; tools like "each_cons" are easy to reach for these types of problems.Part 2
Just for good measure I'll throw in my full file with all the minitest scaffolding that helps me move a little faster when it comes to checking test cases and so on, since this sort of thing has been helpful to me in the past.
Full solution file
here are solutions in j:
part 1:
+/2</\x
part 2:
+/2</\3+/\x
The latter solution could be cleverer.
Here's Befunge. I had to stop pretty quickly last time because the programs got too complex.
Part I
Rust
Solution
Here's my (slightly gross and very lazy) JavaScript implementation
Part 1
Part 2
I've never done this before, but I saw lots of people use it to learn a language so I figured I would check out Rust since it's always the most popular language on the StackOverflow census
Part I
I really wanted to just get the first line to initialize val before entering the loop but couldn't figure out how to do that.
I'd be curious to know what simple ways there might be to improve.
If you make your iterator outside of the for loop you have more control over it, so you could do something like
It seems to me your code would always produce an answer that one one more than the correct answer, since you're starting the comparator value at 0 and the next value will always be higher than that. If you start at
i32::MAX
, the first value will be set in the loop and then you will have correctly initialized the variables without extra code.Ahh, I didn't think about the iterator being outside the loop. I'm coming in to this language totally blind and it's been quite the trip so far so I appreciate the tip!
As for the answer being one higher, I'm not sure but it accepted the answer I got, so not sure.
Like many others, I am using this year's Advent of Code to get more familiar with a language I am interested in, which is Nim for me. Maybe I'll try Rust next year, since it seems popular. Anyway, solutions:
Part 1
Part 2
This is probably not great Nim code, but I've only barely played around with the language so I'm not too worried.
Quick and dirty AWK as usual.
Part 1
Part 2
I was bored, so I decided to make a metaproject involving name choices (which isn't really going anywhere), and this gave me an idea.
I am now officially a Rockstar programmer.
Part 1
Part 2
I probably shouldn't pursue a career in songwriting, though.
The interpreter can be found in the above link.
Python
Late entry, just catching up this weekend! I'll be doing this in Javascript (Node) this year again, since I'm getting better at using the language, and would like to potentially start using it professionally in 2022.
Repo is here
Part 1
Part 2