7
votes
Day 2: Gift Shop
Today's problem description: https://adventofcode.com/2025/day/2
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>
To be honest, I didn't really know what I was doing with this one. Still don't have a clue how to do it in a "smart" way. I just bruteforced it, and it takes a couple seconds to run, which I'm not super happy with, but I'll take the win for tonight. Maybe I'll come back to it in the morning and see if I can figure out a nicer solution. I'm doing a lot of string copying right now, which I think is the main source of overhead.
Solution (Lily)
Brute-forced it again, but learned a lot in doing so!
Had one of my first "aha!" moments in using
string_concat/3backwards to both check if one string was a prefix of another and find the suffix that matched. No idea if it's the most efficient approach, but super neat!Prolog Solution
I made a note to learn about records, as right now I'm just using lists for everything.
This is only part one. I'll do the second later.
Google Sheets
edit: faster
Here are my solutions for both parts in Perl using a feature from the relatively newly released v5.42. I always find that I lean a little heavily on regular expressions for my Advent of Code solutions.
Part 1 (Perl)
Part 2 (Perl)
Slightly compacted versions...
Part 1 (Perl)
Part 2 (Perl)
I didn't post yesterday because I was annoyed at how long it took me. I was much happier today. My part 1 took very little time to write and executed pretty quickly, but part 2 took a little longer. On my machine I averaged about 1.2s. I decided to plug in some threading (not included here) to see if I could get it faster, and got it down to ~230ms. That was a cool win for today!
Part 1
Part 2
The problem today felt about as simple as yesterday, with my logic to find invalid IDs even uglier than the stuff I wrote yesterday. At least, I'm getting a hang of the syntax of Rust and didn't need to spend much time searching the documentation.
Instead of going through all the numbers in each range, I'm sure it's feasible to iterate over the first half of each number and directly check if the resulting repeating ID is inside the range. Thinking about it, I'm feeling like this could even yield a simpler logic than what I currently have.
Part 1 and 2 (Rust)
Edit: Indeed, it's possible to keep track of the part that is going to be repeated and iterate directly over it. Based on the result, it's not exactly simpler, especially since the second part still needs the
is_invalidlogic to avoid double-counting numbers in the range. At least, it's mighty fast: it's running in ~1ms including all the overhead of starting the process and parsing the input while the previous version took ~120ms.Implementation of the sum of invalid IDs over a range for both parts
Edit 2: actually, things can be pushed even further since the sum of invalid IDs (for part 1) in the range
123000..456999is the same as the sum of numbers123..456times 101, which can be computer is O(1) using the fact that the sum of numbers from 1 to n (included) isn*(n+1)/2. Part 2 becomes even more interesting since you also want to exclude all the numbers that are invalid IDs from the sum of the consecutive numbers, which can be done using a recursive call to the function that computes the sum of invalid IDs for a given range. Thus, it's not actually necessary to have anis_validfunction for part 2 either.I haven’t started writing code yet, just looking at the puzzle for now. Something’s bugging me…
Can someone help me understand why
111and999are apparently NOT invalid? I thought I understood the rule but those two are throwing me off.Because in those numbers, the repeated digits are repeated more than twice
D’oh! Thanks!
That should actually make the logic a good deal simpler (at least until the Part 2 complication comes along and undoes everything…)!
You're welcome! good luck and have fun