Advent of Code: Day 2 in the bag!

← Back to Blog

Day two of Advent of Code, day two of a regex coming in handy. I didn't set out to accidentally commit to using a regex per day? But hey here we are. We'll see if we can hit 3/3 after the next challenge drops at 12 UTC. Same as yesterday, we're going to look at my code below and make fun of it a bit. This is after solving part 2, so I had to remove the regex matches from part 1, so ignore the unused imports 😢. I am but a humble, terrible programmer.

import itertools
import re

def repeating_pattern(s):
    n = len(s)
    for L in range(1, n // 2 + 1):
        if n % L == 0:
            sub = s[:L]
            if sub * (n // L) == s:
                return True
    return False



total_invalid_sum = 0

for start, end in list_of_ranges:
    for id_num in range(start, end + 1):
            s = str(id_num)
            if repeating_pattern(s):
                  total_invalid_sum += id_num




print(total_invalid_sum)

This one was a bit more straightforward (no 4k line input file to deal with lol). The main challenge here was identifying numbers that were repeating in sequence. This here is the solution after solving part 2, but thats really just small tweaks to the for loop. The "magic" (if your magicial happens to be inebreated) is in the repeating patterns function. I've been working on incorporating more smaller helpers like this, just to kind of clean things up and keep them readable. Overall I'm actually pretty happy with this one, it runs (we're going to ignore the fact that it's O(n^2) in the worst case here), and it was another really fun exercise. I'm not aiming for elegance here, I'm just happy I'm getting the answers correct.

Now, for the darker portion:

I've finally submitted to social media. I've set up small scrips to post out to X, Mastodon and Bluesky when I publish a new article. I'm not one for much of social media, (if you want to chat, open an issue on Github or email me. Discord if you know me lol), but I do want to share my work with others. Like my TUI for 365 administration - that's a neat tool that could help others along with other projects I have that could be useful. So, setup a small python script that uses the respective APIs to post out a link. Once I create the sites, I'll be able to link the creds and push them out automagically.