7
votes
Day 14: Regolith Reservoir
Today's problem description: https://adventofcode.com/2022/day/14
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>
Part 2 is very slow so I'll have to come back to this. Hurt my knee today and pain > shame
Data
Part 1
Of course this is the refactored
count-sand
after completing part 2 as I didn't want the headache of dealing with the new conditions.Part 2
Now the bottom is the floor, not the void. Why of course I dumbly added 2 to
bottom
."Speedrun"
Attempt 1: ~55% elapsed time.
Mutatis Maledictis. I "caved" in and changed to a mutable hash table which is essentially a re-write. It forced me to make some (ugly) changes to the conditions, but at least both parts together run in under a second now. The beauty of mutable state and only building the data structure once (rather than once for each part) is that part 2 already has the sand in-place from part 1. I don't think the change in rules invalidates that and the results are the same so...
Since it's a pretty major change I'll link the new code here and just show the combined driver for both parts. I'm tempted to try a vector instead of a hash-table, but my knee isn't giving me any relief so that's it for today.
Rust
I enjoyed this one. I was a bit afraid due to the problems reference to 2018 day 17, which I remember finding incredibly hard at the time, but this one wasn’t as bad.
Rust
Elixir
I went through a lot of iterations on this one. Overall I did a few different combinations of
It turned out that the simplest approach was also the fastest: model the data as a flat set, and always drop the sand grains one unit at a time. Other approaches introduced more edge cases and also required repeatedly searching through collections (either the column lists, or the entire set of coordinate pairs), which more than canceled out any efficiency gains from skipping steps.
Both parts
I used multiple function clauses indrop_sand/4
to separate out the 3 main checks (:d
,:l
,:r
) and keep the number of nested branches to a minimum.Part 1 runs in 26 ms, part 2 in 744 ms.