Advent of Code Day 2

Published December 02, 2025

Day 2 of Advent of Code has us identifying invalid product IDs that were put into a system by some mischeivous kids. Our input gives us a range of PIDs that may or may not have one or more invalid PID in them. Our goal is to arrive at the sum of all the invalid PIDs.

For part 1 we're told that an invalid PID is one that is comprised of 2 repeating sequences. This is pretty easy - split the PID in half and if the 2 halves are the same, it's an invalid PID:


if pid[:len(pid) // 2] == pid[len(pid) // 2:]:
    return pid

Run that for each PID in each of the ranges and you're fine.

For part 2 the definition of invalid changed a bit - it's a PID that is made up of 2 or more repeating sequences. For part 1 123123123 wouldn't be an invalid PID but it is for part 2. There are a few ways to do this. I chose to create ever increasing slices of the PID and see if that sequence was repeated through the end:


for stop in (n for n in range(1, (len(pid) // 2) + 1) if len(pid) % n == 0):
    if pid = pid[:stop] * (len(pid) // stop):
        return pid

This is not the most efficient approach, but it works pretty well and lends itself to optimization via some quick multiprocessing (On my machine it took ~1.2 seconds without the multiprocessing and about half that with it.)

A different approach I saw was using regular expressions to do the checks:


part1 = re.compile(r'^(\d+)\1$')
part2 = re.compile(r'^(\d+)\1+$')

While this approach is more efficient in single thread processing, I found it did not optimize when I tried to make the solutuion multithreaded.

To see my full solution head over to https://github.com/brass75/AdventOfCode/blob/main/aoc_2025/day2.py


Previous: Advent of Code Day 1 Next: Advent of Code Day 3