AOC 2025 Day 2 Revisit

 read in about 1 minute

Spent some time yesterday reviewing other people’s solutions to day 2 and I do not recommend my solution.

I found Juhis’ solution and made a part 3 to implement regex instead of my solution. The only changes were importing re, setting the pattern, and using a new function to do the comparison.

import re
INVALID_ID_PATTERN_2 = r'^(\d+)(\1)+$'

def is_invalid(product_id: int) -> re.Match[str] | None:
    return re.search(INVALID_ID_PATTERN_2, str(product_id))

This new setup got the solution so fast that I spent some time today to add timers to both part 2 and part 3 to compare.

Part 2 (my solution) got the correct answer in 38.22 seconds.

Part 3 (Juhis’ solution) got the correct answer in 1.62 seconds.

I think it might be time to learn regex folks. Also, part 3 included all my other code so Juhis’ complete solution may be even faster.

Does anyone have a good resource or way to learn regex?

Listening to: Fall Out Boy - Sugar, We're Goin Down
Christopher Himes

I'm Christopher Himes (he⁠/⁠him), an accomplished tech professional living in Metro Detroit. I'm currently looking for work as a product owner or developer.

More about me

Interactions

@everythingalsocan@techhub.social@twit.social/@tjunge@jeff@phpc.social@mastodon.world/@hamatti@newsmast.community/@programming

Comments

This blog uses a Mastodon and webmentions for comments. You can comment by replying on Mastodon/ActivityPub/Fediverse account or webmention.

Related Posts

Recent Posts