13
votes
Day 3: Mull It Over
Today's problem description: https://adventofcode.com/2024/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>
Oooh smells like an interpreter...
I really hope we get more of this, my all time favorite AoC was building the Intcode VM a few years back.
Ruby
Intcode! that was the first year I participated, and definitely got me hooked. I feel like it's been teased a few times since then but we haven't gotten anything quite the same.
That was my first thought when I read the challenge and I'm looking forward to it!
I feared regex would only get me so far, but it worked for both parts quite well! My Python solutions:
Part 1
Part 2
Someone has written a regex module for Jai, but it doesn't have an in-built way to find all matches, so I wasn't able to use it. Luckily, it wasn't too bad to solve this without regex - I just used a sliding window and verified the instructions manually. A little verbose, maybe, but good enough.
Solution (Jai)
Well this was frustrating. No problems solving it, but every step felt like I was going against the grain. I couldn't get regex groups to work so just fell back on string manipulation and my part 2 didn't feel great.
I'm going to have to dive deeper into Pharo's regex tools, as I'm sure many future days are going to be a pain if I can't get that working well.
Smalltalk Solution
I'm pretty tired tonight (finally remembered to drag myself to the gym a few hours after dinner) so my initial solution is a bit less compact than it could be.
I'll probably go back over it tomorrow and also write a few functions for my utils module.
Rhai Solution
My current utils.rhai
Edit: I added basic regex to Rhai with
new_regex(regex_string)
andcaptures(Regex, input)
functions using the regex crate and made a shorter solution.Rhai Solution
It's always fun to see how the inputs have traps that seem tailored for different ways of approaching the puzzle -- I implemented this twice, because I was curious which approach was nicer, and both times had different problems crop up that weren't a problem for the other approach. The only trap that was missing was a
mul
of the formmul(1234,5678)
.Rust
I first did this using RegEx, but that solution was kind of boring (even though it took me longer than it should have), so here's the hand-rolled evaluator for part 2 I came up with just now:
Doing it this way actually took less time to implement than installing a regex crate and figuring out how to properly escape everything, though of course, doing it for the second time avoids the stupid mistakes you make when you rush.
I did it in with Regex, too, but in Racket, and I might try steal a recursive variant of your solution instead because yours is nice and I'm worried about having to add operations down the line xD
I actually went back to regular expressions after seeing @tjf's solution; now both parts are like 5 lines of code each, which I think is more elegant than having two screen-fulls of manual parsing...
Elixir
I decided to solve this one with regular expressions even though I'll probably regret it if future puzzles keep iterating on this concept.
I learned about the
(?|...)
capture group for this one. Shout out to my pal regex101.com.Elixir's regex implementation has this nice
x
modifier that makes it ignore all whitespace within the pattern, as well as anything after "#" on a line. This lets you break the pattern up into separate lines and add inline comments. Great for readability!Both parts
Python
Step-by-step explanation | full code
I love regex! Using capture groups and
re.findall
made part 1 a 1-liner, which is nice.For today's writeup, I ended up with a (very) basic explanation of regex for people totally unfamiliar with it. So if it's something you've always been interested in, this might be a good place to jump in!
oh yeah, I almost forgot.
I brought everything into A1 for this. This one was really quick and easy. I could probably cut these formulas down... but this year I have decided to not be consumed by AOC.
Part 1
Part 2
I used regular expressions since that's the first thing I though of, though I'm sure I'll regret it and end up giving up early because of it. Even part 2 was confusing me for quite a while.
Racket
Ahh, I quite like the approach here of parsing out all of the valid functions in a single regex and then looping through functions ... it seems more elegant than the string manipulation I did on part 2 (and possibly scales better if this does turn out to be another intcode computer thing?)
I, like many of you, used regex for this, however had to take a quick detour to get "quicklisp" - what appears to be a package manager for common lisp libraries - as common lisp doesn't have a regex library builtin.
That said, part 1 was fairly simple once I'd done that, with the slight convolution that as there was no function to both get capture groups and get multiple matches at the same time I resorted to two passes with the same regex.
Part 2 gave me more trouble, until I remembered that (loop) existed and things are mutable and I could just bang out something to get the active parts of the string in much the same way as I could python. I also looked at a pure regex solution and a range-based solution but this turned out to be a relatively easy implementation by comparison. I pity my friend who is writing this in nix for what I am certain must be a horrible recursive function.
My code
Hello there!
This was a really easy one for me - I've used a bunch of regex in the past, but I did have to figure out some little details of how python's
re
library worked.I also tripped myself up a little bit, and didn't realize that the example for the second part was slightly different than the example for the first part.
Part 2 (and one) solution