Yordi - A Lifelong Journey of Growth

Advent of Code 2025 - Day 2

Here we are again, back for Advent of Code day 2!

Check out my full solution for day 2 at GitHub.

I'll refrain from writing out the whole problem description, as you can find that on the website itself. What it boils down to is this: given a number (or a list of characters, actually), decide if it contains a repeating pattern. For example: 55, 6464 and 123123 all contain a repeating sequence, while something like 242 does not.

Let's take a look at the two parts.

Part one

Part one starts up a bit easier, as it only requires us to find out if a string contains a sequence of characters repeated twice. Or, to rephrase that: if two halves of a string are exactly the same if you split it down the middle. That immediately rules out all strings who have an uneven number of characters. For the remaining set, we can calculate the middle position, extract the two halves and check if they are equal:

id_s = str(id)
mid = len(id_s) // 2
left = id_s[:mid]
right = id_s[mid:]
if left == right:
    # repeated sequence

Part two

Part two is - as always - the tricky one. We now need to check for any repeating pattern, not just two repeating sequences. So, a string of uneven length like 123123123 is now also considered as a string with a repeating pattern.

I did a bit of research on how to do this, and came around this page that offers a neat solution. Let's check the code first:

def is_repeating_string(s):
    return s in (s + s)[1:-1]

This line of code first doubles the string we want to look at, then removes the edges from the doubled string and finally checks if the orginal string is included (a substring of) this "doubled-and-cut-off"-string. If so, we can say the string contains a repeating pattern.

But why does this work?

The magic lies in the fact that if you double a string s (so s + s), it contains all rotations somewhere in the string. It is important to remove the edges, because it is also a fact that a string s is always a substring of its double s + s. For example, a string like abc is always a substring of its double abcabc, but not of its double without edges bcab.

That reduces the quite bloaty check from part one to a clean one-liner, which solves part two.