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
Interactions
Comments
This blog uses a Mastodon and webmentions for comments. You can comment by replying on Mastodon/ActivityPub/Fediverse account or webmention.