< Home


When reading Hackernews I came across this fun little regex puzzle. I wonder if I can solve it.


This is something that can become very… hard very fast. I started nilly willy but after 5 minutes saw that this would prove to be too hard. So I decided, let me first solve one side, continue to another side while keeping the first side compliant and after that the other sides normally will fall in line. We have six sides, I chose to first do the two on the left. As this is really just reading the REGEX and doing the minimal of required input this was done rapidly.

Regex left side

Top side

Ok, a bit harder but still fairly easy. Just do it line per line and only validate left and top.

[^X]*(DN|TE|NI) = Any character that is not X and must end with DN, TE or NI
[RONMHC]*I[RONMHC]* = Any character RONMHC but somewhere you must put an I
.*(..)\1P+ = Must end with P and one time you must have two sequential characters who repeat immediately after
(E|RC|NM)* = Must be E or RC or NM combo
([^MC]|MM|CC)* = Must be MM or CC or anything without the letters M or C
R?(CR)*MC[MA]* = Could start with R, afterwards multiple CR are allowed after each other (or none), MC must come after that and after that letter M or A could be used multiple times or not at all.
.* = Anything goes

Some made my head hurt but after some back and forth and rotating my head, which still gives me neck pain, I got it!

Regex top and left side

Right, top side

Ok, as we must focus a bit more I am breaking up the problem in smaller spaces. While the left side was easy, the right side isn’t anymore as you must now take in account of 2 other sides (left-top and right-bottom).

.*CDD.*RRP.* = Any character 0... times, CCD, any character 0... times, RRP, any character 0... times
(XHH|[^XH])* = XHH or any letter except XH, 0... times
([^CME]|ME)* = ME or any letter except CME
.*RXO.* = Somewhere RXO must happen but this was a real pain in the ass
.*LR.*RL.* = Somewhere LR and RL must happen
.*EU.*ES.* = Somewhere EU and ES must happen

After rearranging some stuff I got top-right completed! But when I look at bottom-right and see it is almost all red… Oh boi!

Regex top-right side

All the rest

As it became a bit too hard to focus on one side only I just started picking out the easy looking ones. Those were mostly filled in except maybe one letter and slow but surely I had the following five regex left

(XR|[^R])* = XR or anything but R
[ROMEA]*HO[UMIEC]* = Any letters from ROMEA, a HO and any letters from UMIEC
.*(.)X\1C\1.* = A repeating letter between X and C
.*(.)(.)(.)(.)\4\3\2\1.* = ABCDDCBA would fit the bill
.*X.*RCHX.* = X with somewhere RCHX

I first solved the .*X.*RCHX.* as the RCHX was already filled in, I saw an opening to change the OOOO to XOXO which solved this one without making any other regex invalid.

After that I did (XR|[^R])*. This line contained two R’s. I needed to remove them or make them XR. The first one could be converted to XR. The second was harder. That R had dependency on two other lines. Changing the I would also cause another failure. But it seemed I could just change the P in that line to I and be able to fulfill all the rules.

Three left!

After doing that suddenly the [ROMEA]*HO[UMIEC]* posed an easy fix with changing another I to H. Fulfilling the HO requirement of the regex.

Two left!

Two left

This one wasn’t easy, I don’t even remember anymore what steps I actually did as I needed to change some other lines. But it felt I just moved some characters around to just comply with the other regex static letters rules.

One left!

One left

The last one… the hardest one. This .*(.)(.)(.)(.)\4\3\2\1.* means that if you have ABCD, it should be followed with DCBA. Not easy in this very carefully set-up regex puzzle. Especially as all other rules were still strict as well.

I looked at what lines still felt strange and noticed that the R*D*M* still had no M’s (no worries, it isn’t like I did a quick glance. This was a 5 minute dead-stare between me and the puzzle). This felt strange so I started there. Oops the ([^CME]|ME)* rule broke because of this but an easy fix solved it. Now I had

I see a pattern!

Do you also see that now on that line we have CMERHEMC. This means the R or H should change and we have our solution! And if we click on them to highlight we easily see that the R is filling in for [^X] and the .*CDD.*RRP.* line is fulfilled at the beginning and end! So we can easily change our R into an H without breaking any other rules!


The solution

This wasn’t easy and I took breaks between them. The regex itself weren’t that hard but fulfilling three of them is. The rules, the shifting around of solutions, hoping you would not break something else, no back button which made sure you needed to remember the previous solution.

Yeah my head hurts now.

I am quite happy I stopped completing side per side as that would be even a bigger brainfuck at the end. At the start that tactic was fine but in the end… no.

I do wonder, is the answer static or are multiple answers available?

As I found this quite fun, here is the original MIT puzzle page and some other regex puzzle websites for me to remember.

Original MIT Puzzle



< Home