9
votes
Day 16: The Floor Will Be Lava
Today's problem description: https://adventofcode.com/2023/day/16
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>
Fun day! While I wasn't aiming for speed this year, I was happy to have most everything go right here and require very little debugging; only took a few minutes to write part 2. My solution for part 2 is not particularly efficient, however, but improving that would likely have required some substantial rewrites to change.
I noticed we haven't had much for graphs or trees yet, which I'm eager to try out.
Haskell Solution
wow, I'm so glad for my grid class! I think I need to add some notion of direction to my pointers, I kinda wanted to but I couldn't figure out a good way to do it. Maybe I'll just do an enum with LEFT, RIGHT, UP, DOWN. What I really need is a
ptr.move_in_direction(steps, wrap)
method so I don't have to keep writing out cases.Anyway, solutions are pretty long again today, so Python solutions on GitHub
I ended up implementing the direction stuff on my Coordinate class, and that has worked pretty well. I posted a link in my you level comment if you want to have a look.
I thought it would be easy enough to use C++, but I already uninstalled CLion so I just use VSCode. That wasn't pleasant to debug, and the complex dict key make it unpleasant to write compared to higher level languages.
Part 2 runtime is 3.36s on my machine with brute force solving.
That was a nice little puzzle. I guess we're in for a hard puzzle tomorrow, then. I'm really hoping we're going to get something related to combinatorial optimization, or at least pathfinding.
Thoughts
This simply came down to following the beam. The only thing I overlooked at first was that I couldn't start with the beam already on the grid (as the instructions had implied) but had to start with the beam shining into the grid from the outside. My top-left corner in the puzzle input had a mirror, and because I had assumed that's where the beam starts, it was ignored. I feel like that must have been deliberate. Did anyone else have that?
Rust
Performance
This is always what gets me, so it feels like the shoe I have been waiting to drop all along.
[Golang solution](https://github.com/firstmustburn/aoc/blob/main/2023/cmd/day16/main go)
I am getting so much mileage out of my grid class. Super glad I put some effort into setting it up.
Discussion
Few bugs I had to work out in my implementation of this one, like getting the directions of the mirror right and stopping infinite loops.
One thing I am grateful for with Go is the grest debugger support in VS code. If I ever end up in a C/C++ environment again, setting that up will be job one.
Only just got to this problem now, but it really didn't take long. Didn't have time to make my solution any faster, but it's not slow enough to be a big deal, so I don't mind keeping it like this. I was honestly a bit disappointed the naïve solution was enough for today, but to be fair, if it wasn't, I might not have been able to finish the problem before midnight, so I guess I should be grateful. Hopefully tomorrow is a bit more interesting.
Solution
Elixir
My code is pretty incomprehensible because I was messing around with macros to define functions with "unusual" names 🌝
The macro tomfoolery essentially allows the solution to turn the grid's tiles into direct function calls, no map lookups or anonymous functions necessary. It didn't really improve performance, but I had fun seeing what kind of nonsense I could get into.
Parts 1 and 2
Step-by-step explanation | full Python code
Very pleased with today. Ended up with a
State
that could generate the nextState
based on its location and direction. Put those in a queue and bail if the next state is out of bounds or has already been seen. I liked the architecture today, which focused on separation of concerns. Also, got to use some features of pattern matching, which is always exciting.Also, spent a bunch of time at the end of the post discussion performance optimizations. Got the (both parts) runtime from ~ 4.6 seconds down to ~ 1.1 with 3 1-line changes!