Discussion thread for day 2 of the Advent of Code - https://adventofcode.com/2025/day/2
Trying to hold off using a macro until at least day 4. Let's see how that pans out.
I could have tidied up Part 2 with a macro, but I didn't.
Not pretty. Some elegant solutions using Regex I need to check out.
This one was more of a thinker for me. Took a little while to figure out what I even wanted to do, but it wasn't too bad to actually build after that!
Part 1:
1) The first steps were relatively straightforward as I used a text to columns and a generate rows to create one row per "potential ID", based on the ID ranges provided in the input.
2) Potentially an unnecessary extra but I created a copy of the ID column but this time stored it as a string, so I didn't need to keep changing the datatype.
3) At this point, I simply extracted the left half and right half of the string separately. I was able to do this by simply taking the length of the string and dividing it by two. Then feeding it into the Left / Right functions.
4) I filtered to the cases where the left and right halves were equal, then summed the values.
Part 2:
Step one and two were the same as in part one, but then things did differ slightly.
1) The key difference between part one and two, is that the "repeated" string could be of any length. However we can deduce, in order for a string to be classed as repeated, it must appear at least twice, hence the length of the repeated pattern cannot exceed, half the length of the overall string. i.e
- If we look at ID 123123, we know that the "repeated string" has to be at most, 3 characters long, (a "repeated string" of 4 characters, would create an answer 8 character long, when repeated, therefore would not work for this example)
- "1", "12", and "123", are the only 3 we need to consider
To consider these cases in isolation I needed one row per case, so I used the generate rows to create these extra lines (counting up, until I got to half the length of the original string):
2) Now I could create the the potential "repeated strings", on their separate lines, by using the Left() function. Then replaced all occurrences of the "repeated string" in the ordinal ID column, with nothing. If the cell in the ID columns was left completely blank, then I knew the pattern must have repeated throughout the whole ID. Using the same example as before I would get the following.
| ID | Repeated String (aka "Substring") | ID after Replacement | |
| 123123 | 1 | 2323 | |
| 123123 | 12 | 33 | |
| 123123 | 123 | < - Nothing remaining therefore the ID must have contained the repeated string (and nothing else) |
Edit: If I were to go back and redo the question, having seen other peoples solutions, I would like to use the regex "\1$". It appears to represent one repetition of the group previously defined. I'd never seen it before today so thanks all for the new regex trick!
Suspiciously straightforward...
I first used Text to Columns to split on the comma, then split to rows on the dash. I then generated rows between the two parts of the ID:
I then used some sexy Regex to identify first matches of two characters, then two or more:
Then simply summed the IDs for each match.
Whenever I do the first couple of days quickly I feel it's written specifically to hurt me.
M.
I, like others, found this challenge to be easier mentally to figure out than Day 1, but I could see where others might disagree. It's a different skillset to solve this than the first day's. Anyway...
Here’s my solution:
Narrative:
It was easy enough to parse the data, splitting it by comma, then by the dash. From there, filling in the ranges, etc.
Task 1: It dawned on me pretty quickly that odd numbers wouldn’t satisfy the pattern, so I used Mod() to get rid of those, then simply split the string in half using the string length and compared the first half to the second half. Easy enough.
Task 2: Turns out my regex skills aren’t as creative as others. With the helpful hint that people were using Regex, I did some research and found a suitable solution to identify the Invalid ID’s.
An alternative that I considered was just splitting the string, which had a max value of 10 into separate streams and then rejoining them towards the end. The auspice of building out that many tools seemed like more work than exploring the Regex, so that’s the route I went in the end.
And now back to the breathing easy, waiting until Day 3. Cheers, -Jay
As @PhilipMannering famously said "RegEx is the sex"
